|
|
||||||
|
#1
|
|
|
|
|
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 |
|
|
|
#2
|
|
|
|
|
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? |
|
#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 ... 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. |
|
#4
|
|
|
|
|
Paul Welter wrote:
[..] > 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 |
|
#5
|
|
|
|
|
> 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 |
|
#6
|
|
|
|
|
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 |
|
#7
|
|
|
|
|
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" <oliver> wrote in message news:2916 [..] |
|
|
| Similar Threads | |
| How to retrieve a generic method with GetMethod (reflection) ? How to retrieve a generic method with GetMethod Code ------------------- #public static void Sort<T>( T[] array, Comparison<T> comparison [System.Reflection.MethodInfo]... |
|
| Generic method which returns generic type Hi, I am curious how to make generic method which will return generic type. Something like: class Test { T Get<T>() { if(T is int) { return 1; //return int } |
|
| Generics Question - how to implement a method on a generic thatreturns the same type as the generic I am having a really bad time with this one. For some reason, I can't seem to remember how to implement this (elegantly) in c# ... so I thought I would ask the... |
|
| Generic type converter class. Cant call static method of type T. in a generic class, can I code the class so that I can call a static method of the generic class T? In the ConvertFrom method of the generic TypeConvert class I want... |
|
| Type.GetMethod() with generic method parameters: a hen-or-egg problem Hi there, I wonder how to get a MethodInfo using System.Reflection.Type::GetMethod when the method has a parameter which is a generic method parameter. Consider this... |
|
|
All times are GMT. The time now is 04:30 PM. | Privacy Policy
|