从VB调用C#dll [英] Call C# dll from VB

查看:190
本文介绍了从VB调用C#dll的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用VB项目中的C#DLL,到目前为止,我还没有问题,但是在更新DLL版本后,调用函数时出现一些编译错误,我不确定问题是否来自可选参数或输出参数。

I am using a C# DLL from an VB project, so far I hadn't problems, but after updating DLL version I have some compilation errors when calling functions, I'm not sure if the problem comes from optional parameters or output parameters.

简而言之,我的问题与这一个

In short, my problem is the opposite to this one.

这是DLL中函数定义的示例(如果我解决了这个问题,它发生在其他函数调用中的一个,它是一个BIG dll):

This is an example of function definition in DLL (if I fix this one it happens in other function calls, it's a BIG dll):

public static bool RequestCode(string countryCode, string phoneNumber, out string password, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "")
public static bool RequestCode(string countryCode, string phoneNumber, out string password, out string response, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "")
public static bool RequestCode(string countryCode, string phoneNumber, out string password, out string request, out string response, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "")

这是我从VB拨打的电话(所有人都抛出错误):

This is my call from VB (all of them throw error):

result = ThatLibrary.ThatClass.RequestCode(country, telephone, pass, cc, method)

Or

result = ThatLibrary.ThatClass.RequestCode(country, telephone, pass, method)

result = ThatLibrary.ThatClass.RequestCode(country, telephone, pass, method, Nothing, Nothing, Nothing, "204", "")

result = ThatLibrary.ThatClass.RequestCode(countryCode:=pais, phoneNumber:=telefono, password:=password, method:=metodo, id:=Nothing, language:=Nothing, locale:=Nothing, mcc:="204", salt:="")

这是错误消息:


错误3'RequestCode'不明确,因为存在多种使用此名称的成员在类 ThatClass中。

Error 3 'RequestCode' is ambiguous because multiple kinds of members with this name exist in class 'ThatClass'.

寻找解决方案几天后,我正在考虑将所有项目移至C#,但这是一个这项艰巨的任务,所以我希望有一个我错过的简单解决方案...

After some days looking for a soluction I'm considering moving all my project to C# but this is a huge task so I hope there's a simple solution I missed...

推荐答案

在这种情况下,您必须命名所有参数以在VB中工作。
最后一个方法调用(命名所有参数)可使用以下测试:

You have to name all your parameters for this case to work in VB. Your last method call (naming all parameters) works with the following test:

C#dll:

namespace ClassLibrary1
{
    class ThatClass
    {
        public static string RequestCode(string countryCode, string phoneNumber, out string password, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "") { password = ""; return ""; }
        public static string RequestCode(string countryCode, string phoneNumber, out string password, out string response, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "") { password = "";  response = ""; return ""; }
        public static string RequestCode(string countryCode, string phoneNumber, out string password, out string request, out string response, string method = "sms", string id = null, string language = null, string locale = null, string mcc = "204", string salt = "") { password = ""; response = ""; request = ""; return ""; }
    }
}

VB项目,引用C#dll:

VB project, referencing the C# dll:

Class ThisClass
    Sub testing()
        Dim country As String = "USA"
        Dim telephone As String = "555-555-5555"
        Dim pass As String = ""
        Dim cc As String = ""
        Dim method As String = ""
        Dim test As String = ClassLibrary1.ThatClass.RequestCode(countryCode:=country, phoneNumber:=telephone, password:=pass, method:=method, id:=Nothing, language:=Nothing, locale:=Nothing, mcc:="204", salt:="")
    End Sub
End Class

这篇关于从VB调用C#dll的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆