c# - Is it posible to a make generic return type from member -


is possible create method requires generic type, , return type based on member of given type

something this

class exampleclass {     public t.returntype send<t>() t : classwithtype     {         ...     } }  abstract class classwithtype {    internal abstract type returntype { get; } }  

and if it's not possible alternative.

thanks in advance

since comments said want send packet via socket , each packet has different return type - can encode return type in packet class itself:

public abstract class packet<tresponse> {     // other members here     public abstract tresponse decoderesponse(byte[] raw); }  public class intpacket : packet<int> {     public override int decoderesponse(byte[] raw) {         // decode         return 0;     } } 

then send method becomes:

static tresponse send<tresponse>(packet<tresponse> packet) {     // send, got response     byte[] raw = getresponse();     return packet.decoderesponse(raw); } 

and call becomes just:

int response = send(new intpacket()); 

Comments

Popular posts from this blog

Command prompt result in label. Python 2.7 -

javascript - How do I use URL parameters to change link href on page? -

amazon web services - AWS Route53 Trying To Get Site To Resolve To www -