c# - How could I declare list of generics with different and unknown types and How could I initialize a generic with a type only known at runtime? -


this 2 part question.

firstly
have class called componentlist looks this:

public class componentlist<t> : list<t> t : component {  } 

now want create empty list of typeless componentlists:

list<componentlist<>> masterlist = new list<componentlist<>>(); 

i error because componentlist wants generic type specified though i'm not trying initialize componentlists yet (just masterlist contains them). how declare masterlist of componentlists without initializing componentlists (as plan initialize them during runtime types known @ runtime)? after all, masterlist needs contain componentlists of different generic types, not one.

secondly
know 1 has been asked before, can't seem wrap head around concepts of of proposed solutions.

as know, have list<> called masterlist list of componentlists (a custom class). componentlist generic class of undefined type (that constrained being subtype of component).

in following example, i'm trying check if componentlist generic type (referenced masterlist) same class (the class code's being called from).

if (masterlist[i].gettype() == typeof(componentlist<this.gettype()>)) {  } 

problem is, code meant automatically called unknown child classes, not parent class. componentlist generic type needs compared type of child class, not base class. hence "this.gettype" inplace of hardcoded name of actual base class.

the this.gettype() type passed in componentlist<> returns "type expected" error apparently because gettype() returns compile time type, not runtime type (which need).

so how runtime type? , if can't, best alternative accomplishing i'm trying do? (i've heard bit called reflections might me don't understand).

you cannot determine generic types @ runtime. c# (all .net, actually) designed way on purpose. compiler treats generic type virtually same way explicitly defined type.

consider following:

class mygenericclass<t> {     public static t myproperty { get; set; } }  class myintclass {     public static int myproperty { get; set; } }  class usethem {     public void test()     {         // both jit'ed same machine code         var foo = mygenericclass<int>.myproperty;         var bar = myotherclass.myproperty;     } } 

thus, must provide type generic class jit knows compile.

possible alternative if of possible types possible end being generic type similar (inherit same base class or implement similar interface), can instantiate generic class interface or base class generic type:

list<componentlist<componentbase>> masterlist = new list<componentlist<componentbase>>(); // -or- list<componentlist<imycomponent>> masterlist = new list<componentlist<imycomponent>>(); 

i have hunch should able define common interface or base class little creative refactoring. :)


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -