引用具有相同命名空间和类型的两个 DLL
Reference Two DLLs with the same namespaces and types
我有同一个 DLL 的两个版本,即 LibV1.dll 和 LibV2.dll。这两个库具有相同的命名空间和类型,但不兼容。我需要能够在 VB.Net 项目中同时引用两者,以便将数据从旧版本升级到新版本。这似乎在 C# 中很容易解决,但我读过的所有内容都表明 VB.Net 中没有解决此问题的方法。事实上,我从 2011 年看到这个 post,这证实了这一点。但是我想知道在过去 4 年中是否有任何变化现在可能使这成为可能?
抱歉,我本来希望将此贴上评论,但 SO 阻止了我这样做。
据我所知,VB没有添加C# Aliasing功能,但是你说VB.Net没有解决方案的说法是不正确的。
您在 2011 年引用的 post 指出您使用反射作为解决方法。我认为最简单的方法是选择您希望哪个 DLL 具有 Intellisense 支持并添加对该 DLL 的引用。然后,您可以使用 Reflection.Assembly.LoadFile 获取对第二个 DLL 的引用,并在该实例上使用 CreateInstance 方法创建对所需 class 的对象引用。您将使用后期绑定来处理 class 实例。或者,您可以使用反射来获取所需的 MethodInfo's/PropertyInfo's/etc。并通过它们处理 class 实例,但我认为这比使用后期绑定要多得多。
编辑以添加示例。
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
我有同一个 DLL 的两个版本,即 LibV1.dll 和 LibV2.dll。这两个库具有相同的命名空间和类型,但不兼容。我需要能够在 VB.Net 项目中同时引用两者,以便将数据从旧版本升级到新版本。这似乎在 C# 中很容易解决,但我读过的所有内容都表明 VB.Net 中没有解决此问题的方法。事实上,我从 2011 年看到这个 post,这证实了这一点。但是我想知道在过去 4 年中是否有任何变化现在可能使这成为可能?
抱歉,我本来希望将此贴上评论,但 SO 阻止了我这样做。
据我所知,VB没有添加C# Aliasing功能,但是你说VB.Net没有解决方案的说法是不正确的。
您在 2011 年引用的 post 指出您使用反射作为解决方法。我认为最简单的方法是选择您希望哪个 DLL 具有 Intellisense 支持并添加对该 DLL 的引用。然后,您可以使用 Reflection.Assembly.LoadFile 获取对第二个 DLL 的引用,并在该实例上使用 CreateInstance 方法创建对所需 class 的对象引用。您将使用后期绑定来处理 class 实例。或者,您可以使用反射来获取所需的 MethodInfo's/PropertyInfo's/etc。并通过它们处理 class 实例,但我认为这比使用后期绑定要多得多。
编辑以添加示例。
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