Pages

Wednesday, May 25, 2011

Calling a function written in C# from the Vb.net having the same name for two functions but differ by case having the same signature

Calling a function written in C# from the Vb.net having the same name for two functions but differ by case having the same signature.
In this case, we need to use Reflection.
Suppose my C# assembly code looks like:
namespace Test
{
 
public class Class1
  {
    public string hello()
    {
      return "hello";
    }
    public string Hello()
    {
      return "Hello";
    }
  }
}

To call the function "Hello" in vb.net do this:
Dim obj As New Test.Class1
Dim ReturnValue As Object
ReturnValue = obj.GetType.InvokeMember("Hello", _
              System.Reflection.BindingFlags.Instance Or _
              BindingFlags.Public Or BindingFlags.InvokeMethod, _
              Nothing, obj, 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.

No comments:

Post a Comment