I’m trying to create an instance of a class that I can add to a list in a generic way..
I know the type of class that needs to be made and i’ve been able to make an object of the class using the code below but I have not found a way to create a cast that will allow me to add this to a list.. any ideas?
T is the same as objType
public static List<T> LoadList(string fileName, Type objType)
{
List<T> objList = new List<T>();
object o = Activator.CreateInstance(objType);
objList.Add((**o.GetType()**)o);
return objList;
}
if theres a better way of doing this too im open to ideas 🙂
Just use the non-generic API:
I’m also assuming that
typeis a generic type-parameter; just to say:typeis confusing for this;TorTSomethingSpecificwould be more idiomatic.Also: this line needs fixing: