If I create an instance of a class with a string name, e.g.
public static object GetInstance(string strFullyQualifiedName) {
Type type = Type.GetType(strFullyQualifiedName);
if (type != null)
return Activator.CreateInstance(type);
}
which returns an object:
object myClass = GetInstance("MyClassName");
How can I then pass this object 'myClass' as T to a method?
public static List<T> CreateListAndAddEmpty<T>() where T : new() {
List<T> list = new List<T>();
list.Add(new T());
return list;
}
var myList = CreateListAndAddEmpty<myClass>();
It doesn't accept myClass?
'myClass' is a variable but is used like a type
Any ideas how to create a class from string name, which can be used as T?
Thanks,