c# - Construct generic instance with a generic parameter and a normal parameter -
i've got following struct , need create instance of through system.reflection . big issue have generic , non generic parameter. have use system.reflection because it'll called in loop t can vary. i've looked @ this , unabled work second parameter (int tag). i'm sorry have ask basic question.
struct pair<t> { public pair(t value,int tag) {...} }
so need magic in this:
object createpair(object o,int tag) { return *somemagic* }
edit: solution making struct public in combination first answer. if struct nested in generic struct, both answers throw argumentexception, independent of input. i'm sorry not knowing affect of nesting.
the reason linked answer works because overload of activator.createinstance
hooks parameterless, public constructors, known default constructor.
however, same method has overloads include signature accepts parameters.
public object createpair(object t, int tag) { var type = t.gettype(); var targettype = typeof(pair<>).makegenerictype(type); return activator.createinstance(targettype, t, tag); }
Comments
Post a Comment