Calling a generic method with Type

D

D2

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<T> GetList<T>(object obj)
{
T objInstance = default(T);
//objInstance = CreateObject<T>(ref objInstance);

List<T> x = 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
 
D

David Browne

D2 said:
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;
}

Get a System.Reflection.MethodInfo for GetList, and call
MethodInfo.MakeGenericMethod(myType) to create a specialized MethodInfo.

MethodInfo getList = myType.GetMethod(...);
MethodInfo getListOfMyType = getList.MakeGenericMethod(myType);
object rv = getListOfMyType.Invoke(null,obj);

David
David
 
D

D2

Get a System.Reflection.MethodInfo forGetList, and call
MethodInfo.MakeGenericMethod(myType) to create a specialized MethodInfo.

MethodInfo getList= myType.GetMethod(...);
MethodInfo getListOfMyType =getList.MakeGenericMethod(myType);
object rv = getListOfMyType.Invoke(null,obj);

David
David

David,

Thanks for your inputs. Keeping performance aside, it works for me :)
Thanks again.

Cheers!
D2
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top