Hi,
I have a requirement where I need to call a generic method without
knowing the class name i.e. I'm getting class name from xml file.
Given below is a replica of the scenario we are having. The generic
method shown is just to explain the scenario, actually its somewhere
outside.
private void Form1_Load(object sender, EventArgs e)
{
Assembly assembly = Assembly.LoadFrom("full path to dll,
read from xml file"); //we are getting full path of dll from a xml
file
Type myType = assembly.GetType("class name got from xml
file"); //the class name we are getting from xml file
object obj = Activator.CreateInstance(myType);
//GetList<myType>(obj); //here we have issue calling the
generic method. Generic method requires the class name which we are
getting at runtime.
return;
}
private List<TGetList<T>(object obj)
{
T objInstance = default(T);
//objInstance = CreateObject<T>(ref objInstance);
List<Tx = new List<T>();
objInstance = CreateObject<T>(ref objInstance, typeof(T));
x.Add(objInstance);
x.Add(objInstance);
x.Add(objInstance);
return x;
}
private T CreateObject<T>(ref T obj, Type _objType)
{
if (!(_objType == null))
{
obj = (T)Activator.CreateInstance(_objType);
}
return obj;
}
I'm just wondering if there is a way by which we can call the generic
method GetList without actually providing the class name? or we can
provide the Type of class name???
Thanks for your time,
Dapi