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

Popular posts from this blog

commonjs - How to write a typescript definition file for a node module that exports a function? -

openid - Okta: Failed to get authorization code through API call -

ios - Change Storyboard View using Seague -