wpf - Class name as string in defining a collection in c# -
i have class name string got using type
type type = myvar.gettype(); string _classname = type.tostring(); // getting class name
my question how use string _classname here in code below?
var data = this.itemssource observablecollection<**_classname**>()[2];
here itemssource generic.
thanks in advance.
you can using reflection , activator.createinstance
method:
type type = myvar.gettype(); string classname = type.tostring(); type generictype = type.gettype(classname); type observablecollectiontype = typeof(observablecollection<>); type constructedtype = observablecollectiontype.makegenerictype(generictype); var constructedinstance = activator.createinstance(constructedtype);
Comments
Post a Comment