Type.GetMethod with Generic method

P

Paul Welter

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
 
R

realfun

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?
 
J

Jon Shemitz

Paul said:
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.
 
O

Oliver Sturm

Paul said:
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
 
P

Paul Welter

You might be able to GetMethod("GetCollection said:
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
 
O

Oliver Sturm

Paul said:
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
 
T

Ted Miller

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 said:
Paul said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top