How to use two DLL's with the same namespace and the same class name in VB.NET

1

this time I need to import into a VB project two DLLs that have the same namespace, the same class name and the same methods but are completely different in terms of functionality since one is more recent than the other (they are DLL's to perform billing), the only difference they currently have is the name of the file. Try to import them but they do not work for me, they tell me that they are ambiguous when trying to call the methods.

Imports libfacturacion
Imports libfacturacion_com
...
Dim x As New libfacturacion
Dim y As New libfacturacion_com

After importing them and declaring them in the variables as a new instance when trying to access the methods, they mark me as ambiguous. For some strange reason, although they are in different variables, I do not respect the access to the methods.

    
asked by user4028047 12.07.2017 в 21:22
source

2 answers

0

You can use two versions in the same executable, as long as you configure it in the app.config or web.config (the assembled and signed thing is yours, look at the attribute publicKeyToken of assemblyIdentity and set the assemblies in a subfolder of bin each with its version.

<configuration>
    <runtime>
        <assemblyBinding xmlns="urn:schemas-microsoft-com:asm.v1">
            <dependentAssembly>
                <assemblyIdentity name="Factura.API" publicKeyToken="12ab3cd4e5f6abcd"
                  culture="neutral" />
                <codeBase version="1.0.0.0" href="v1\Factura.API.dll" />
                <codeBase version="2.0.0.0" href="v2\Factura.API.v2.dll" />
            </dependentAssembly>
        </assemblyBinding>
    </runtime>
</configuration>

Then you should create two AppDomain different and load in each of them the specific version. To redirect the specified assembly (through the full path) you must implement a "proxy" class. This way you can distinguish in your code the version you want to use at all times.

An example can be

Module Module1

    Sub Main()

        Dim myAppDomainSetup As New AppDomainSetup()
        myAppDomainSetup.ApplicationBase = System.Environment.CurrentDirectory
        Dim myEvidence As Evidence = AppDomain.CurrentDomain.Evidence
        Dim myDomain As AppDomain = AppDomain.CreateDomain("MyDomain", myEvidence, myAppDomainSetup)
        Dim myDomain2 As AppDomain = AppDomain.CreateDomain("MyDomain2", myEvidence, myAppDomainSetup)

        Dim type As Type = GetType(Proxy)
        Dim proxy = DirectCast(myDomain.CreateInstanceAndUnwrap(type.Assembly.FullName, type.FullName), Proxy)


        Dim pathToDll = System.IO.Path.Combine(myAppDomainSetup.ApplicationBase, "v1\Factura.API.dll")
        Dim pathToDll2 = System.IO.Path.Combine(myAppDomainSetup.ApplicationBase, "v2\Factura.API.v2.dll")

        Dim assembly = proxy.GetAssembly(pathToDll)
        Dim assembly2 = proxy.GetAssembly(pathToDll2)

        Dim typeFacturacion1 As Type = assembly.GetType("Factura.API.Tipo1", True)
        Dim facturacion1 = Activator.CreateInstance(typeFacturacion1)

        Dim typeFacturacion2 As Type = assembly2.GetType("Factura.API.Tipo1", True)
        Dim facturacion2 = Activator.CreateInstance(typeFacturacion2)

        AppDomain.Unload(myDomain)
        AppDomain.Unload(myDomain2)

        Console.ReadKey()
    End Sub


End Module

Public Class Proxy
    Inherits MarshalByRefObject
    Public Function GetAssembly(assemblyPath As String) As Assembly
        Try
            Return Assembly.LoadFile(assemblyPath)
        Catch generatedExceptionName As Exception
            ' throw new InvalidOperationException(ex);
            Return Nothing
        End Try
    End Function
End Class
    
answered by 13.07.2017 / 11:33
source
0

In C # there is the concept of external alias for these situations. In vb.net you should not have problems with Imports , using this format:

Imports NombreFicheroDll.Namespace.Clase

and then you must define a variable with the class:

Dim Dll1 As New NombreFicheroDll.Namespace.Clase
Dll1.Metodo()
    
answered by 13.07.2017 в 09:06