Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ambiguous match found when accessing dll

I am trying load a function in a dll. The dll is loaded but just at the place of invoking the function, I am getting an exception

Ambiguous match found

Here is the code snippet.

Assembly dll = Assembly.LoadFrom(DLLPATH);
if (dll != null)
{
    Type Tp = dll.GetType("ABCD.FooClass");
    if (Tp != null)
    {
        Object obj = Activator.CreateInstance(Tp);

        if (obj != null)
        {                            
            List = (List<String>)obj.GetType().GetMethod("Foo").Invoke(obj, null);
        }
        else
        {
            Console.WriteLine("obj is null");
        }
    }
    Console.WriteLine("Type is null");
}
else
    Console.WriteLine("Dll is not loaded");

Console.ReadKey();

The method which I am calling (i.e Foo), does not accept any parameters and it is an overloaded method. Is that the place where I am going wrong or is it some other place?

Is there another way to invoke such methods which does not accept any parameters? I tried the solution posted here but it is not working.

like image 334
Pankaj Kolhe Avatar asked Nov 30 '22 01:11

Pankaj Kolhe


2 Answers

If there is an overload and you want to invoke the method with no parameters this is the correct solution:

MethodInfo mi = obj.GetType().GetMethod("Foo", new Type[] { });
like image 167
lupok Avatar answered Dec 05 '22 06:12

lupok


The method Type.GetMethod(string methodName) throws the exception you mentioned if there is more than one method with the specified name ( see this MSDN topic ). As Foo is an overload as you say I suspect that there are multiple Foo methods in the same DLL. If you have for example the methods :

IList<string> Foo()

IList<string> Foo(object someParameter)

The method GetMethod(string methodName) can not determine which one you want to have. In this case you should use the method GetMethods and determine the correct method on your own.

like image 32
Tobias Breuer Avatar answered Dec 05 '22 06:12

Tobias Breuer