473,395 Members | 1,468 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Calling a generic method with Type

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
2 2302


"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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Gibby Koldenhof | last post by:
Hiya, I'm setting up some code in the spirit of Design Patterns, OOP, etc. All nice and well, it handles pretty much all OO style things and serves my purposes well. There's one last final...
7
by: Jorgen Haukland, Norway | last post by:
Hi, I have created a Java webservice which runs in IBM WebSphere appserver. I take the WSDL-file and create a VS.NET WinForm application and calls the service running on my PC and everything...
1
by: jens Jensen | last post by:
Hello , i'm calling a webservice generated with oracle webservice java tools. I'm not able to add a web reference to a .net client the usual way with visual studio 2005. I was therefore...
4
by: taub.adam | last post by:
Hi, Can generics be used to call a static method of a class? The below example gives an error in my generictest class. Anybody know why? class foo { static public void Test() {...
3
by: Otis Mukinfus | last post by:
I've been wrestling with this for a while and can't figure it out. I have a generic method with the following signature: //this compiles OK public abstract class DataMethod { public...
5
by: Anders Borum | last post by:
Hello! I'm trying to call a generic method by providing the T parameter at runtime, as opposed to compile time (i.e. by specifying the T parameter in the method call). I was under the impression...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
0
by: =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post by:
"Anders Borum" wrote: Hi Anders, I'm afraid the GetMethod() does not currently support filtering on generic parameters so you will have to loop through the existing methods using...
6
by: Ole Nielsby | last post by:
VC has a __cdecl specifier which allows functions and methods to be called with varying parameter count. (I understand this is the default for functions in general but in VC, instances use...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.