Hello,
I'm having a method in csharp code that is calling another method in VB using MethodInfo.GetMethod and MethodInfo.InvokeMember. The problem is the MethodInfo.InvokeMember worked but the MethodInfo.GetMethod did not. Can someone point out what is the problem here.
Csharp code:
1) This DOES NOT WORK
public virtual object ExecuteMethod()
{
object result = null;
try
{
string methodName= "myMethod";
object[] args = {"One", "Two"};
System.Type methodsType = this.GetType();
MethodInfo info = methodsType.GetMethod(methodName,
BindingFlags.Instance | BindingFlags.Public |
BindingFlags.Static | BindingFlags.ExactBinding);
result = info.Invoke(this, args);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
return result;
}
The error from the above code:
Object of type 'System.String' cannot be converted to type 'System.String[]'
2) This WORKS
public virtual object ExecuteMethod()
{
object result = null;
try
{
string methodName= "myMethod";
object[] args = {"One", "Two"};
System.Type methodsType = this.GetType();
result = methodsType.InvokeMember(methodName,
BindingFlags.DeclaredOnly |
BindingFlags.Public |
BindingFlags.NonPublic |
BindingFlags.Instance |
BindingFlags.InvokeMethod,
null,
this,
args);
}
catch (Exception exc)
{
MessageBox.Show(exc.Message);
}
return result;
}
VB Code:
Public Sub myMethod(name as String, ByVal ParamArray extraInfos() As String)
MessageBox.Show("Inside myMethod method")
End Sub
Thanks,