473,406 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

Call vb method from csharp

1
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,
May 4 '10 #1
0 1119

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: David N | last post by:
Hi All, I just wonder if in C#, I can develop a user defined control that can call its parent function which is not yet developed. For example, how do I make my user control call a...
4
by: Charles | last post by:
Hello, I have created a class file: Admin.cs with a namespace of cs101.Code.nsAdmin. From my ASP page: Login.aspx.cs how do I reference Admin and then execute one of its methods "public...
24
by: Jazper | last post by:
hi i have this problem. i made a class deverted by CRootItem with implementation of IDisposable-Interface. i made a test-funktion to test my Dispose-Method.... but when set a breakpoint in my...
1
by: Tim Begin | last post by:
I am attempting to use the ThreadPool.SetMinThreads method as shown in the MSDN example code: int minWorker, minIOPort; int newWorker,newIOPort; ThreadPool.GetAvailableThreads(out minWorker, out...
4
by: Tamir Khason | last post by:
Is it possible (as was in VB - CallByName) to call function which name was generated. Example: private static void DS_function() { } private static void FD_function() {
8
by: ThomasR | last post by:
I understand that virtual methods on inherited objects are slower than non-virtual methods because of the indirection required to support the call. However, when looking at IL code produced by...
3
by: Janaka | last post by:
When using object inheritance is it possible to call a method of the base class more than 1 object below the inherited class? For example, assume that the Labrador class inherits all the way down...
1
by: Jeffrey Kingsley | last post by:
I would like to cancel a call to BeginInvoke of a delegate (i.e. kill the thread the call was made on). This would be done when a timeout occures for the waitone call to the WaitHandle object of...
4
by: Dom | last post by:
In the days of VB, it was possible to do the following: "Call Button1_Click()", although there was some argument about whether this was asking for trouble. Is it possible to do it in CSharp? ...
7
by: Andrus | last post by:
How to get syntactically correct signature which compiles for code template grneration ? I tried code below but it creates syntactically incorrect signature. Andrus. using System; using...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.