简体   繁体   中英

How to call a function defined in C# with same params,return type and same name but in different case from a VB.NET project

I have a class project a.dll which is 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 like this

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

But how can I call this from a VB.Net Project ? I am not in a position to use Visual Studio right now. So its very helpful if someone will provide an answer.

EDIT 1

I am having a VB.NET project and I need to add the reference of a 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 reference to the dll and so I can able to call the functions.

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

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

Your best bet is to use reflection here - VB simply cannot determine which function you are calling since in VB 'add' is identical to 'Add'.

Here's what I did to test it (I'm not really 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})

if your C# code is CLS compliant, you can simply add a reference to it to your vb.net project.namespace , public members in the DLL can be acessed


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

You can use free online convesion tools also .
eg : 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: * As per above link Note: *

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

using System;

// etc

[assembly: CLSCompliant(true)]

update3:

as i said above you want to ensure that you C# code is CLS compliant .
C# is case-sensitive, where VB.NET is not . you are violating CLS guidelines
Please Note update2 to ensure compliant nature

At last i found the article that guided me long ago

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

update4:

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

Actually VB will not let you call either Add method by name. You will get a compiler error indicating the name Add is ambiguous. Giving two public members names which differ only by case is not CLS-compliant. Call up the person who wrote the C# class and tell them to use CLS-compliant names. If they won't do that, then you will have to resort to reflection.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM