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
Post a Comment