GetMethod causes Ambiquous match found exception


A

Andrus

Class MyClass contains a number of FindAll() methods in parent classes:

class MyClass: ActiveRecordBase<MyClass> {... }

public abstract class ActiveRecordBase<T> : ActiveRecordBase {
protected ActiveRecordBase();
public static T[] FindAll();
public static T[] FindAll(params ICriterion[] criteria);
public static T[] FindAll(DetachedCriteria criteria, params Order[] orders);
public static T[] FindAll(Order order, params ICriterion[] criteria);
public static T[] FindAll(Order[] orders, params ICriterion[] criteria);
....
}

public abstract class ActiveRecordBase : ActiveRecordHooksBase {
protected ActiveRecordBase();
protected internal static Array FindAll(Type targetType);
protected internal static Array FindAll(Type targetType, params ICriterion[]
criteria);
protected internal static Array FindAll(Type targetType, DetachedCriteria
detachedCriteria, params Order[] orders);
protected internal static Array FindAll(Type targetType, Order[] orders,
params ICriterion[] criteria);
....


I need to invoke MyClass parameterless FindAll() method using Reflection.
I tried the following code but GetMethod() returns Ambiquous match found
exception.
How to run parameterless FindAll() method ?
How to add required method signature to GetMethod() parameters or other
solution ?

Type t = Type.GetType("MyClass, MyDll");

// this line causes Ambiquous match found exception :
MethodInfo mi = t.GetMethod("FindAll",
BindingFlags.Public |
BindingFlags.FlattenHierarchy |
BindingFlags.Static);

IList<object> list = (IList<object>)mi.Invoke(null, null);

Andrus
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Andrus said:
I need to invoke MyClass parameterless FindAll() method using Reflection.
I tried the following code but GetMethod() returns Ambiquous match found
exception.
How to run parameterless FindAll() method ?
How to add required method signature to GetMethod() parameters or other
solution ?

Type t = Type.GetType("MyClass, MyDll");

// this line causes Ambiquous match found exception :
MethodInfo mi = t.GetMethod("FindAll",
BindingFlags.Public |
BindingFlags.FlattenHierarchy |
BindingFlags.Static);

Use the overload of GetMethod which specifies the types of the
parameters:

MethodInfo mi = t.GetMethod("FindAll", // Name
BindingFlags.Public | // Flags
BindingFlags.FlattenHierarchy |
BindingFlags.Static,
null, // Binder (default)
new Type[0], // Parameter types (none)
null); // Parameter modifiers (ignored)
 
A

Andrus

Use the overload of GetMethod which specifies the types of the
parameters:

MethodInfo mi = t.GetMethod("FindAll", // Name
BindingFlags.Public | // Flags
BindingFlags.FlattenHierarchy |
BindingFlags.Static,
null, // Binder (default)
new Type[0], // Parameter types (none)
null); // Parameter modifiers (ignored)

Jon,

thank you.

Is'nt it better to use Type.EmptyTypes instead of new Type[0] ?

Andrus.
 
Ad

Advertisements

J

Jon Skeet [C# MVP]

Andrus said:
thank you.

Is'nt it better to use Type.EmptyTypes instead of new Type[0] ?

Well, I find new Type[0] easier to understand personally - but it's
entirely your decision :)
 

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