473,796 Members | 2,607 Online
Bytes | Software Development & Data Engineering Community
+ 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)Met hodInfo.GetCurr entMethod();

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

// To get the instantiated generic method call I can use
MakeGenericMeth od().
// 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.MakeGeneri cMethod(new Type[] {
g.GetType() });
// info3.GetString () returns "Void f[Int32](Int32)"

// I also tried to work with RuntimeMethodHa ndle and
// MethodInfo.GetM ethodFromHandle ()
// but I always get the handle for the unbounded method.

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

Jan 4 '06 #1
4 2458
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.Reflecti on;

public class Test
{
static T ShowMethod<T>()
{
MethodInfo info = (MethodInfo)Met hodBase.GetCurr entMethod();
Console.WriteLi ne(info.MakeGen ericMethod(type of(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.GetGeneric Parameters() 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.GetGeneric Parameters() 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
RuntimeMethodHa ndle 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
1784
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 hashtable that supports (or implements) generics? I haven't tried generics hands-on, but I would assume that once you don't have to do casting anymore, things could speed up quite a bit.
17
3329
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 way they are. ***** Summary & Questions ***** In a nutshell, the current .NET generics & .NET framework make it sometimes difficult or even impossible to write truly generic code. For example, it seems to be impossible to write a truly generic
2
2876
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 'System.Collection.Generic.IList'. When performing something like: 'if (typeof(IList<>).IsAssignableFrom(propertyInfo.Type))' where 'propertyInfo' obviously refers to the 'InvoiceLines', the result is always 'false' because indeed 'IList<object>' and...
1
2286
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 type.Assembly.GetType(name,false,false) method. It is return as null. Can anybody tell can help me to find the basic reason of this issue? this is the sample code that I used. using System; using System.Collections.Generic;
1
6096
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 GenericCollection<Address> mAddresses;
2
4245
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 class, Base, with a constraint on the generic parameter. A generic class, RelTable, with a constraint on two generic parameters. A reflection code that just enumerates types, methods and methods'
0
1215
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 assembly, for example a generic method parameter declared as: System.Collections.Generic.List<int> the full name of that parameter type returned by reflection is :
18
2637
by: riftimes | last post by:
Hello, would you help me to find totorials with examples about generics and Dictionary thank you.
7
6357
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 that list, but it is not explicitly creating an instance of MyClass, but instead using the Activator to create an instance based on it's type name. How can I accomplish this in C# using reflection?
0
9531
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
10187
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10018
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9055
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7553
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6795
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5446
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5578
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3735
muto222
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.