Problem
I faced this issue today. I wanted to call a function written in C# from my VB.NET code, but I found out that the coder of the library had not followed Microsoft guidelines for interoperability and had used same name for two functions differing only by case having the same signature. I didn't have access to the C# code, so was helpless as VB.NET does not allow me to use any of these functions. It gives a compile time error:
Overload resolution failed because no accessible 'f<function> is most specific for these arguments: <function names> : Not most specific
Solution
In this case, we need to use Reflection. At least, I could only find this solution.
Suppose my C# assembly code looks like the following:
namespace CompTest
{
public class Class1
{
public string cameLate()
{
return "He came late";
}
public string camelAte()
{
return "Camel Ate Him";
}
}
}
We can call the function camelAte
in VB.NET as follows:
Dim CSClass As New CompTest.Class1
Dim ReturnValue As Object
ReturnValue = CSClass.GetType.InvokeMember("camelAte", _
System.Reflection.BindingFlags.Instance Or _
BindingFlags.Public Or BindingFlags.InvokeMethod, _
Nothing, CSClass, Nothing, Nothing, Nothing, Nothing)
TextBox1.Text = ReturnValue
InvokeMember
function used here Invokes the specified member, using the specified binding constraints and matching the specified argument list. You can find more information on MSDN.
To pass parameters, create an array of Object
. Insert parameters required to call the function in the array. Pass the array as a parameter in the InvokeMethod
function.