I have class name as string that I got using TYPE
Type type = myvar.GetType();
string _className = type.ToString(); // getting the class name
My question is how to use this string _className here in the code below?
var data = this.ItemsSource as ObservableCollection<**_className**>()[2];
Here ItemsSource is generic.
Thanks in advance.
You can do that using reflection and the 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);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.