How to call a function defined in C # with the same parameters returned by the type and the same name, but in another case from a VB.NET project

I have a class.dll project that compiled in C #. It contains the following code.

public class MyClass{ public int Add(int i,int j) { return (i+j); } public int add(int i,int j) { return (i+j)*2; } } 

from a C # project, I can call these functions as follows

 public class MyOtherClass{ MyClass mcls=new MyClass(); mcls.Add(1,2); mcls.add(2.3); } 

But how can I call it with the VB.Net Project ? I cannot use Visual Studio right now. Therefore, it is very useful if someone provides an answer.

EDIT 1

I have a VB.NET project and I need to add a link to C # dll ( say dll contains MyClass ). So that I can call two methods ( Add(int,int) , add(int,int) ). But in VB.NET, this is case sensitive. Is there any way to achieve this?

EDIT 2

Suppose I added a link to the dll and so I can call the functions.

 Dim myObj as New MyClass myObj.Add(1,2) myObj.add(1,2) 

If this code works, how does the compiler identify the correct function?

+6
source share
3 answers

It is best to use reflection here - VB simply cannot determine which function you are calling, since in VB the "add" is identical to the "Add".

Here is what I did to test it (I'm not quite sure which "BindingFlags" you need to combine here):

 Dim mcls As New [MyClass] Dim t As Type = mcls.GetType() Dim x = t.InvokeMember("add", BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod, Nothing, mcls, New Object() {1, 2}) Dim y = t.InvokeMember("Add", BindingFlags.DeclaredOnly Or BindingFlags.Public Or BindingFlags.NonPublic Or BindingFlags.Instance Or BindingFlags.InvokeMethod, Nothing, mcls, New Object() {1, 2}) 
+5
source

if your C # code is compatible with CLS, you can simply add a link to it in your vb.net project.namespace, public elements in the DLL can be executed


More here
http://www.christec.co.nz/blog/archives/290

You can also use free online streaming tools.
for example: http://www.developerfusion.com/tools/convert/vb-to-csharp/

update:

Read this http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/dda8d7cb-0fa1-43d6-a90f-6c4bed0b40bb

 Dim c As New MyClass() MsgBox(c.Add(1, 2)) 'if only Add() is Available 


Update2: * In accordance with the link above Note: *

Also, in a C # project, add the following to AssemblyInfo.cs:

 using System; // etc [assembly: CLSCompliant(true)] 

Update3:

as I said above, you want to make sure that the C # code complies with the CLS requirements .
C # is case sensitive where VB.NET is not . you are violating CLS recommendations
Please consider update2 to ensure compatible character.

I finally found an article that had been leading me for a long time

http://www.devarticles.com/c/a/C-Sharp/Making-Your-Code-CLS-Compliant/

update4:

Another scale http://msdn.microsoft.com/en-us/magazine/cc163750.aspx

+2
source

In fact, VB will not allow you to call the Add method by name. You will get a compiler error indicating that the name Add ambiguous. Providing two public member names that differ only in case does not meet CLS requirements. Call the person who wrote the C # class and suggest that they use CLS-compatible names. If they do not, you will have to resort to reflection.

+1
source

Source: https://habr.com/ru/post/947174/


All Articles