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

Type.GetMethod with Generic method

I'm trying to get a method using Type.GetMethod. There are two methods with
that same name, one is a standard method, the other is a Generic method.
How do I get the Generic method? Is there a BindingFlag that will only get
the Generic one? Here is an example ...

[TestClass()]
public class UserTest
{
public ArrayList GetCollection()
{ return new ArrayList(); }

public List<T> GetCollection<T>()
{ return new List<T>(); }

[TestMethod()]
public void GetCollectionTest()
{
Type genType = typeof(UserTest);
MethodInfo info = genType.GetMethod("GetCollection");
MethodInfo genInfo = info.MakeGenericMethod(typeof(User));
object obj = genInfo.Invoke(this, null);
List<User> users = obj as List<User>;
}
}

If I run this code, I get an AmbiguousMatchException. If I remove the first
GetCollection, it works. Any ideas?

thanks
~ Paul
Nov 17 '05 #1
6 17094
I haven't read through the c#2.0 spes, but i notice that GetCollection
and GetCollect<T> have the same signature, only differ by return type,
is it a confliction?

Nov 17 '05 #2
Paul Welter wrote:
I'm trying to get a method using Type.GetMethod. There are two methods with
that same name, one is a standard method, the other is a Generic method.
How do I get the Generic method? Is there a BindingFlag that will only get
the Generic one? Here is an example ...


I don't think there's a GetMethod overload that will distinguish your
GetCollection() from your GetCollection<T>() - so far as I can see,
you'll have to call GetMethods(), then filter by Name and
ContainsGenericParameters.

--

www.midnightbeach.com
Nov 17 '05 #3
Paul Welter wrote:
I'm trying to get a method using Type.GetMethod. There are two methods with
that same name, one is a standard method, the other is a Generic method.
How do I get the Generic method? Is there a BindingFlag that will only get
the Generic one? Here is an example ...

[TestClass()]
public class UserTest
{
public ArrayList GetCollection()
{ return new ArrayList(); }

public List<T> GetCollection<T>()
{ return new List<T>(); }

[TestMethod()]
public void GetCollectionTest()
{
Type genType = typeof(UserTest);
MethodInfo info = genType.GetMethod("GetCollection");
MethodInfo genInfo = info.MakeGenericMethod(typeof(User));
object obj = genInfo.Invoke(this, null);
List<User> users = obj as List<User>;
}
}

If I run this code, I get an AmbiguousMatchException. If I remove the first
GetCollection, it works. Any ideas?


You might be able to GetMethod("GetCollection<>"), because it's really
the non-constructed Generic type information you are interested in. I
guess that should suffice for Reflection to make the distinction between
the two methods.

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #4
> You might be able to GetMethod("GetCollection<>"), because it's really the
non-constructed Generic type information you are interested in. I guess
that should suffice for Reflection to make the distinction between the two
methods.


GetMethod("GetCollection<>") does not work. The documentation has a note
that says...

"The name parameter cannot include type arguments. For example, the C# code
GetMethod("MyGenericMethod<int>") searches for a method with the text name
"MyGenericMethod<int>", rather than for a method named MyGenericMethod that
has one generic argument of type int."

Of cource it doesn't say how to search for a generic.

thanks
~ Paul
Nov 17 '05 #5
Paul Welter wrote:
GetMethod("GetCollection<>") does not work. The documentation has a note
that says...

"The name parameter cannot include type arguments. For example, the C# code
GetMethod("MyGenericMethod<int>") searches for a method with the text name
"MyGenericMethod<int>", rather than for a method named MyGenericMethod that
has one generic argument of type int."

Of cource it doesn't say how to search for a generic.


You are right. I confused this with the way you can look at Generic
types for classes, e.g. "Type type = typeof(Collection<>)".

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog
Nov 17 '05 #6
You might be able to use GetMethod("GetCollection`1").

But to figure it out, why not write some code that prints out the names of
all the methods in your class (with Type.GetMethods())?

"Oliver Sturm" <ol****@sturmnet.org> wrote in message
news:OI**************@TK2MSFTNGP14.phx.gbl...
Paul Welter wrote:
I'm trying to get a method using Type.GetMethod. There are two methods
with that same name, one is a standard method, the other is a Generic
method. How do I get the Generic method? Is there a BindingFlag that
will only get the Generic one? Here is an example ...

[TestClass()]
public class UserTest
{
public ArrayList GetCollection()
{ return new ArrayList(); }

public List<T> GetCollection<T>()
{ return new List<T>(); }

[TestMethod()]
public void GetCollectionTest()
{
Type genType = typeof(UserTest);
MethodInfo info = genType.GetMethod("GetCollection");
MethodInfo genInfo = info.MakeGenericMethod(typeof(User));
object obj = genInfo.Invoke(this, null);
List<User> users = obj as List<User>;
}
}

If I run this code, I get an AmbiguousMatchException. If I remove the
first GetCollection, it works. Any ideas?


You might be able to GetMethod("GetCollection<>"), because it's really the
non-constructed Generic type information you are interested in. I guess
that should suffice for Reflection to make the distinction between the two
methods.

Oliver Sturm
--
omnibus ex nihilo ducendis sufficit unum
Spaces inserted to prevent google email destruction:
MSN oliver @ sturmnet.org Jabber sturm @ amessage.de
ICQ 27142619 http://www.sturmnet.org/blog

Nov 17 '05 #7

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

Similar topics

16
by: AxlsPixel | last post by:
Hi All, I have a class (called CTestClass) within which I have a method (Foo). This method has the following signature: Foo(int x, int y, ref int z) I am attempting to use reflection to...
1
by: phancey | last post by:
I am trying to invoke a web service method dynamically. I have created a generic function that takes a method name, string of parameters and calls the web method using System.Reflection: ...
0
by: Marek | last post by:
Hi I need to call various functions in a native C++ DLL (FORTRAN eventually too) - passing integers, doubles, (pointers and arrays to both of these as well) and ultimately structures too. I was...
8
by: Ympostor | last post by:
Why can't I do this (it gives a syntactic error):? foreach (System.Reflection.PropertyInfo oProp in oObject.GetType().GetProperties()) { if ((oProp.PropertyType.IsGenericType) &&...
10
by: Marc Gravell | last post by:
Given a generic method "of T", is there a good way of ensuring that any static ctor on T has executed? Following code demonstrates (TestClass1) that via generics you can use the Type instance long...
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. ...
6
by: =?ISO-8859-1?Q?Norbert_P=FCrringer?= | last post by:
Hello, Imagine, that I've got a string variable containing a value of each possible value type (string, int32, double, ...). Now I want to convert the string to an object of the right type,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
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...

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.