473,396 Members | 1,891 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,396 software developers and data experts.

Calling generic method with a T parameter determined at runtime

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 that the CLR was able to infer the T type
dynamically - perhaps I was wrong or simply "asking the wrong question"? :-)

public void CallingMethod()
{
User sender = new User();

// does not compile
this.GenericMethod<sender.GetType()>(sender);

// compiles just fine
this.GenericMethod<User>(sender);
}

public void GenericMethod<T>(object sender)
{
..
}

Thanks in advance.

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
May 19 '07 #1
5 26060
To use a Type at runtime you need to use MakeGenericMethod

http://msdn2.microsoft.com/en-us/lib...ricmethod.aspx

Marc

May 19 '07 #2
Thanks Marc, I'll look into it :-)

--
With regards
Anders Borum / SphereWorks
Microsoft Certified Professional (.NET MCP)
May 19 '07 #3

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
To use a Type at runtime you need to use MakeGenericMethod

http://msdn2.microsoft.com/en-us/lib...ricmethod.aspx

Marc
I would also like to thank you.

- Michael S
May 23 '07 #4

"Marc Gravell" <ma**********@gmail.comwrote in message
news:11**********************@l77g2000hsb.googlegr oups.com...
To use a Type at runtime you need to use MakeGenericMethod

http://msdn2.microsoft.com/en-us/lib...ricmethod.aspx

Marc
Not sure what that buys you, unless you are emitting IL dynamically, because
if you call through reflection, you won't have any sort of type safety.
You'd be better off using a base class or interface instead of generics (or
use the base class as the generic type argument).

For this prototype, I can't see any value in having a generic:
public void GenericMethod<T>(object sender)
{
..
}

The only purpose of a generic is to have the return type or argument type
vary, within the function it is still treated according to the constraints,
not the actual type.

In other words, instead of using MakeGenericMethod and reflection, just call
GenericMethod<object>()

and cast any results as needed.
May 23 '07 #5
Ben Voigt <rb*@nospam.nospamwrote:
Not sure what that buys you, unless you are emitting IL dynamically, because
if you call through reflection, you won't have any sort of type safety.
You'd be better off using a base class or interface instead of generics (or
use the base class as the generic type argument).

For this prototype, I can't see any value in having a generic:
public void GenericMethod<T>(object sender)
{
..
}

The only purpose of a generic is to have the return type or argument type
vary, within the function it is still treated according to the constraints,
not the actual type.
There are other possible uses - there's nothing to say that the results
can't depend on T, even if nothing passed or returned uses T directly.
It's effectively like passing a parameter of type Type in that case,
except that you can add constraints etc.

I agree it would be a pretty rare case, but it's not impossible.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
May 23 '07 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Robert Zurer | last post by:
This method works but FxCop rightly complains "Generic methods should provide type parameter" http://www.gotdotnet.com/team/fxcop/docs/rules.aspx?version=1.32...
1
by: Dotnet Gruven | last post by:
I've posted this in the adonet group, however it was suggested I might have better luck here.... ============================================================= I'm trying to use a typed dataset and...
12
by: acb | last post by:
Hi, I have a list of different objects in a <List> Structure. There is only one category of each kind of object. Current I have the following methods: public static Flag GetFlagObj() {...
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...
2
by: D2 | last post by:
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. ...
1
by: pekbob1 | last post by:
Hi Everybody I have the error (ObjectDataSource 'ObjectDataSource1' could not find a non-generic method 'UpdatePre' that has parameters: Comp, Job_Number, Request_Date, Customer, Contact_Name, Tel,...
0
by: Cirene | last post by:
Using Visual Studio I created a DataSet using the GUI (XSD file). Trying to use a tiered methodology I called the functions from my BLL. Namespace Zzz.BusinessLogicLayer #Region "DAL Access"...
3
by: Anders Borum | last post by:
Hi I need to invoke a generic method determined at runtime. The method has two arguments, a string and a generic type that is constrained to a struct: public void Add<T>(string key, T value)...
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...
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
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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:
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

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.