c# - Generic Class:: Type Parameter <T> Property Inheritance -
my code:
class classx; class class2 : classx; class class3 : classx;
class2 , class3 have no association each other whatsoever, except them sharing same base class. class2 , class3 don't share same properties, data types or amount of properties.
now, want able is:
class class<t> : t; static void main() { class<class2> foo = new class<class2>(); foo.class2_property = value; console.read(); }
i'm aware code not possible, there work around?
in c++, can use operator overload so:
public : operator return_data_type const;
thanks in advance.
ps: opened question right here, wasn't how ask question, now. left open in-case finds answer question through answers i've gotten.
edit #1:
i've got packet class has shared packet properties , methods, , different packets have own header , different content. i'm changing packet system. want packet packetclass name of specific packet, example logininfo. logininfo class contain properties:
public packetheader header; public string username; public string password;
therefore:
packet<logininfo> foo = new packet<logininfo>(); foo.username = "user"; byte[] buf = foo.toarray(); // part of packet<t> class
that's meant each class has it's own properties. hope gives got idea of i'm trying do.
you want rethink implementation, isn't c# designed do. can hacky things make work, it's not pretty:
class class<t> t : new() { public t value = new t(); }
this creates 'container' 'base' type, relying on constraint type t
must have parameterless constructor.
then can do:
class classx {} class class2 : classx { public int test = 10; }
then can consume fake 'derived' class this:
class<class2> foo = new class<class2>(); var test = foo.value.test;
you have make fields you're interested in on class2
public work.
Comments
Post a Comment