471,599 Members | 1,780 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,599 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 16548
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

16 posts views Thread by AxlsPixel | last post: by
10 posts views Thread by Marc Gravell | last post: by
2 posts views Thread by D2 | last post: by
6 posts views Thread by =?ISO-8859-1?Q?Norbert_P=FCrringer?= | last post: by
reply views Thread by =?Utf-8?B?TW9ydGVuIFdlbm5ldmlrIFtDIyBNVlBd?= | last post: by
reply views Thread by leo001 | last post: by

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.