Larry G
Larry G

Reputation: 1

C# reflection and methods ambiguity

I have a assembly which contains a class. That class has two methods

public IEnumerable Invoke();
public IEnumerable<T> Invoke<T>();

I dynamically load the assembly

Assembly as = Assembly.Load("MyAssemblyName, Version= 6.1.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35")
Type type = as.GetType("MyClass");
object myObject= Activator.CreateInstance(type);

IEnumerable test= (IEnumerable)myObject.GetType().InvokeMember("Invoke", BindingFlags.InvokeMethod, null, myObject, null);

I want this method is called: public IEnumerable Invoke();

when I run the program I got an error: Ambiguous match found

So what needs to do to remove the ambiguity, so the non-generic method to be called?

Thanks in advance.

Upvotes: 0

Views: 1427

Answers (1)

Sriram Sakthivel
Sriram Sakthivel

Reputation: 73482

You can find the method by calling GetMethods and check ContainsGenericParameters is false. Optionally you could also check for parameter count to zero.

var method = yourType.GetMethods()
    .Where(x => x.Name == "Invoke")
    .First(x => !x.ContainsGenericParameters);
method.Invoke(myObject, null);

Upvotes: 9

Related Questions