【问题标题】:Call C# dll from VB从 VB 调用 C# dll
【发布时间】:2014-03-18 14:15:43
【问题描述】:

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

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

这是 DLL 中函数定义的一个示例(如果我修复了这个,它会发生在其他函数调用中,它是一个 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 打来的电话(它们都抛出错误):

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#,但这是一项艰巨的任务,所以我希望有一个我错过的简单解决方案......

【问题讨论】:

  • 我不知道,但是 C# 有一种方法可以指定命名的可选参数。如果 vb.net 有这个,那可以解决你的问题。但实际上,您需要清理这些签名。
  • 确实如此。向那个 C# 程序员发送仇恨邮件。
  • @SLaks,这不是我的 DLL,Hans Passant 是个好主意,不过他很乐意免费给我那个 DLL ;-)。 crashmstr 会尝试的
  • @K.Weber:我认为这可能是因为我使用 VB 中定义的方法在 VB 中进行了测试,而不是 C#。

标签: c# .net vb.net dll


【解决方案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

【讨论】:

  • 我同意这个应该工作,所以我一定是缺少其他东西,我会再试一次
  • 问题似乎出在 Visual Studio 版本中,使用 DLL 的项目在 Visual Studio 2008 中(:-S 是的!),我去了 VS2012,它现在正在工作
猜你喜欢
  • 2012-06-23
  • 1970-01-01
  • 1970-01-01
  • 2010-11-02
  • 1970-01-01
  • 1970-01-01
  • 2016-02-09
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多