c# - make generic types in code variable -
i want create container class works kind of directory, multiple keys , possibility assign several values 1 key. number of possible keys should variable. want through creating every key-type in inputtypes new dictionary contains key , index of value in values' list.
class samplecontainer<tvalue> { public samplecontainer(params type[]inputtypes) { foreach(type t in inputtypes) { ls.add(new dictionary(t,int));//won't compile } values=new list<tvalue>(); } list<dictionary< ???,int>> ls;/*object ??? doesn't work, fill in keep "generic"*/ list<tvalue>values; }
try
list<idictionary> ls = new list<idictionary>(); ls.add(typeof(dictionary<,>).makegenerictype(typeof(int), typeof(bool)) .getconstructor(type.emptytypes) .invoke(null) idictionary); there side-effects though, idictionary interface uses object key/value pairs. may cause boxing when attempting retrieve value (because returns object types).
maybe can think of neat way , keep strong typing, closest can think come asking do.
Comments
Post a Comment