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.
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[] { });
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With