By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,627 Members | 2,244 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,627 IT Pros & Developers. It's quick & easy.

Calling a generic method with Type

P: n/a
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<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

Mar 7 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a


"D2" <dh*****@gmail.comwrote in message
news:11*********************@30g2000cwc.googlegrou ps.com...
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

Mar 7 '07 #2

P: n/a
D2
On Mar 7, 7:25 pm, "David Browne" <davidbaxterbrowne no potted
m...@hotmail.comwrote:
"D2" <dhap...@gmail.comwrote in message

news:11*********************@30g2000cwc.googlegrou ps.com...
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 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

Mar 8 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.