Hello,
I'm having a method in csharp code that is calling another method in VB using MethodInfo.GetM ethod and MethodInfo.Invo keMember. The problem is the MethodInfo.Invo keMember worked but the MethodInfo.GetM ethod 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.Get Method(methodNa me,
BindingFlags.In stance | BindingFlags.Pu blic |
BindingFlags.St atic | BindingFlags.Ex actBinding);
result = info.Invoke(thi s, 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.Inv okeMember(metho dName,
BindingFlags.De claredOnly |
BindingFlags.Pu blic |
BindingFlags.No nPublic |
BindingFlags.In stance |
BindingFlags.In vokeMethod,
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,
I'm having a method in csharp code that is calling another method in VB using MethodInfo.GetM ethod and MethodInfo.Invo keMember. The problem is the MethodInfo.Invo keMember worked but the MethodInfo.GetM ethod 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.Get Method(methodNa me,
BindingFlags.In stance | BindingFlags.Pu blic |
BindingFlags.St atic | BindingFlags.Ex actBinding);
result = info.Invoke(thi s, 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.Inv okeMember(metho dName,
BindingFlags.De claredOnly |
BindingFlags.Pu blic |
BindingFlags.No nPublic |
BindingFlags.In stance |
BindingFlags.In vokeMethod,
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,