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:
((IList)objList).Add(o);
I'm also assuming that type
is a generic type-parameter; just to say: type
is confusing for this; T
or TSomethingSpecific
would be more idiomatic.
Also: this line needs fixing:
List<type> objList = new List<type>(); // <=== not new List<Object>