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

Calling generic method with a T parameter determined at runtime

P: n/a
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
Share this Question
Share on Google+
5 Replies


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

P: n/a
Thanks Marc, I'll look into it :-)

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

P: n/a

"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

P: n/a

"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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.