从VB调用C#dll

时间:2014-03-18 14:15:44

标签: c# .net vb.net dll

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

简而言之,我的问题与this one相反。

这是DLL中函数定义的一个例子(如果我解决了这个问题,它会在其他函数调用中发生,它是一个很大的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调用的(所有这些都抛出错误):

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

或者

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”类中存在多种具有此名称的成员。

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

1 个答案:

答案 0 :(得分:1)

您必须将此案例的所有参数命名为VB。 您的上一次方法调用(命名所有参数)适用于以下测试:

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:

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