引用两个具有相同命名空间和类型的 DLL

use*_*445 5 .net vb.net dll

我有同一个 DLL 的两个版本,即 LibV1.dll 和 LibV2.dll。这两个库具有相同的命名空间和类型,但不兼容。我需要能够在 VB.Net 项目中同时引用两者,以便将数据从旧版本升级到新版本。这在 C# 中似乎很容易解决,但我读到的所有内容都表明 VB.Net 中没有解决此问题的方法。事实上,我看到2011年的这篇文章,证实了这一点。但我想知道过去 4 年是否发生了任何变化,使得现在这一切成为可能?

TnT*_*nMn 3

抱歉,我本来希望粘贴此评论,但 SO 阻止我这样做。

据我所知,VB还没有添加C# Aliasing功能,但是你关于VB.Net中没有解决方案的说法是不正确的。

您引用的 2011 年帖子指出您可以使用反射作为解决方法。我认为最简单的途径是选择您想要智能感知支持的 DLL,并添加对该 DLL 的引用。然后,您可以使用 Reflection.Assembly.LoadFile 获取对第二个 DLL 的引用,并在该实例上使用 CreateInstance 方法创建对所需类的对象引用。您可以使用后期绑定来处理该类实例。或者,您可以使用反射来获取所需的 MethodInfo's/PropertyInfo's/等等。并通过它们来处理类实例,但我认为这比使用后期绑定要做更多的工作。

编辑添加示例。

Sub Test()
    ' assume you chose Version 2 as to reference in your project
    ' you can create an instance of its classes directly in your code 
    ' with full Intellisense support

    Dim myClass1V2 As New CommonRootNS.Class1

    ' call function Foo on this instance
    Dim resV2 As Int32 = myClass1V2.foo

    ' to get access to Version 1, we will use Reflection to load the Dll

    ' Assume that the Version 1 Dll is stored in the same directory as the exceuting assembly
    Dim path As String = IO.Path.GetDirectoryName(Reflection.Assembly.GetExecutingAssembly.Location)

    Dim dllVersion1Assembly As Reflection.Assembly
    dllVersion1Assembly = Reflection.Assembly.LoadFile(IO.Path.Combine(path, "Test DLL Version 1.dll"))

    ' now create an instance of the Class1 from the Version 1 Dll and store it as an Object
    Dim myClass1V1 As Object = dllVersion1Assembly.CreateInstance("CommonRootNS.Class1")

    ' use late binding to call the 'foo' function. Requires Option Strict Off
    Dim retV1 As Int32 = myClass1V1.foo
End Sub
Run Code Online (Sandbox Code Playgroud)