4

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.

Community
  • 1
  • 1
Pankaj Kolhe
  • 241
  • 1
  • 4
  • 20

4 Answers4

11

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[] { });
lupok
  • 1,025
  • 11
  • 15
8

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.

fero
  • 6,050
  • 1
  • 33
  • 56
Tobias Breuer
  • 825
  • 1
  • 11
  • 19
3

Thanks guys for your help.!!

As I told you, the method (i.e FOO) which I was calling, is overloaded. I did not used GetMethod() properly I suppose. Now, I found a solution using GetMethods() function.

I changed my code in following way and it worked.!!

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)
      {                            
         MethodInfo[] AllMethods = obj.GetType().GetMethods();
         MethodInfo Found = AllMethods.FirstOrDefault(mi => mi.Name == "Foo" && mi.GetParameters().Count() == 0);
         if (Found != null)
             List = (List<String>)Found.Invoke(obj, null);           
      }
      else
        Console.WriteLine("obj is null");       
   }
    else
     Console.WriteLine("Type is null");
 }
  else
     Console.WriteLine("Dll is not loaded");
Pankaj Kolhe
  • 241
  • 1
  • 4
  • 20
0

Thanks.

My "Ambiguous match found" was I had a textbox in the ASCX(frontend) named Bio, a data element named Bio in a listview <%# DataBinder.Eval(Container.DataItem, "Bio")%> and I named a string variable Bio in .CS.

No build errors or "redlines" but generated an error at runtime. I renamed the variables differently and the error went away. The prgrammer who wrote the code didn't follow naming conventions such as BioTxt. This would of eliminated the error.

WebPro
  • 171
  • 1
  • 2