473,473 Members | 1,736 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Reflection & Generics

Hi All,
I would like to get MethodInfo of current instantiated generic method
call.

Consider the following function call:
C.f<int>(5);

Where f is defined as follows:
class C
{
static public void f<G>(G g)
{
// Try to get the MethodInfo of f() with the current generic
parameters indtantiation
}
}

I have tried the following code inside f():

// ThisMethodInfo represents the unbounded method,
// e.g., info.ToString() returns "Void f[G](G g)"
MethodInfo info =
(MethodInfo)MethodInfo.GetCurrentMethod();

// Using the stackTrace gives the same unbounded info
System.Diagnostics.StackTrace stack = new
System.Diagnostics.StackTrace();
MethodInfo info2 =
(MethodInfo)stack.GetFrames()[0].GetMethod();

// To get the instantiated generic method call I can use
MakeGenericMethod().
// However, I'm looking for general code, which is not
based on the specific
// method signature (e.g., G may be as a return value, or
as a generic parameter for
// one of the function argument...)
MethodInfo info3 = info.MakeGenericMethod(new Type[] {
g.GetType() });
// info3.GetString() returns "Void f[Int32](Int32)"

// I also tried to work with RuntimeMethodHandle and
// MethodInfo.GetMethodFromHandle()
// but I always get the handle for the unbounded method.

Any help will be appreciated!
Thanks in advanced,
Dubi.

Jan 4 '06 #1
4 2426
Dov Tendler wrote:
I would like to get MethodInfo of current instantiated generic method
call.


<snip>

You can use typeof(T) within a generic method:

using System;
using System.Reflection;

public class Test
{
static T ShowMethod<T>()
{
MethodInfo info = (MethodInfo)MethodBase.GetCurrentMethod();
Console.WriteLine(info.MakeGenericMethod(typeof(T) ));
return default(T);
}
static void Main()
{
int i = ShowMethod<int>();
}
}

Jon

Jan 4 '06 #2
Hi Jon,
I need to write a general code for any method, without knowing what the
generic parameters are. In other wrods, I would like to use the
MethodInfo without using T explicitly.
Using info.GetGenericParameters() returns the generic types of the
method, but I don't know how to convert it to the real instantiated
type.
Many Thanks for your help,
Dubi.

Jan 4 '06 #3
Dov Tendler wrote:
I need to write a general code for any method, without knowing what the
generic parameters are. In other wrods, I would like to use the
MethodInfo without using T explicitly.
Using info.GetGenericParameters() returns the generic types of the
method, but I don't know how to convert it to the real instantiated
type.


I *suspect* you can't do this.

Out of interest, what's the overall goal here?

Jon

Jan 4 '06 #4
Hi Jon,
Your last example gives me the hint I'm looking for.
Since I use instrumentation techinques, I may use the ldtoken MSIL
instruction on each method generic parameter to generate the relevant
RuntimeMethodHandle of the instantiated type.
Many Thanks for your help,
Dubi.

Jan 4 '06 #5

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

Similar topics

13
by: Anders Borum | last post by:
Hello! Now that generics are introduces with the next version of C#, I was wondering what kind of performance gains we're going to see, when switching from e.g. the general hashtable to a...
17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
2
by: Marc | last post by:
Given a class 'Invoice' with a property 'public IMyColl<IInvoiceLine> InvoiceLines' where 'IMyColl<T> : IList<T>' i would like to detect by reflection that 'InvoiceLines' is a...
1
by: Rathish P S | last post by:
Hi friends, I am trying with Reflection and Generics in C#. But I get confused with some of these new features. I am trying to get the type of generic parameter with the...
1
by: uttara | last post by:
I have a generic collection which I am using in classes to store a collection of embedded objects. Class Employee: IEntity { Private string mName; Private int mEmployeeID; …. Private...
2
by: Wiktor Zychla [C# MVP] | last post by:
Could anyone confirm/deny that following is a bug (or at least an "unexpected behaviour")? If this is not a bug, I would be glad for a short explanation or a workaround. Issue: A generic...
0
by: Konrad Kaczanowski | last post by:
Hi all, I'm creating code generator for wrappers of some c# classes. With the introduction of c# 2.0 and generics the following problem arises. When encountering generic types anywhere inside the...
18
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
7
by: =?Utf-8?B?TXJOb2JvZHk=?= | last post by:
Say I have a class that has a generics List as follows: public List<MyClassmyClassList = new List<MyClass>(); and I want to create another class which tries to add an element of MyClass 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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
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...
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,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.