VB.NET embedded DLL in another DLL as embedded resource?

前端 未结 2 1583
故里飘歌
故里飘歌 2021-01-03 06:37

I have seen this done in C#, such as here although, I cannot seem to figure out how to do this in VB.NET. For some background, I have created a custom ComboBox control as a

2条回答
  •  礼貌的吻别
    2021-01-03 07:09

    I took a little bit of a different approach. I wanted something that would pretty much auto-initialize and dynamically load embedded assemblies as they were being used. I also wanted to avoid loading multiple instances of an assembly that already existed in the current AppDomain. The code below accomplishes all of those for me.

    Imports System.Reflection
    Imports System.Runtime.CompilerServices
    ''' 
    ''' This class initializes a special AssemblyResolve handler for assemblies embedded in the current assembly's resources. 
    ''' To auto initialize create a variable as a New EmbeddedAssemblyResolverClass in any class using an embedded assembly.
    ''' 
    Public Class EmbeddedAssemblyResolverClass
    Implements IDisposable
    
    ''' 
    ''' Initialization flag.
    ''' 
    ''' [Boolean]
    Public ReadOnly Property Initialized As Boolean
    
    ''' 
    ''' Raised when successfully initialized.
    ''' 
    Public Event Initilized()
    
    ''' 
    ''' Raised when successfully uninitialized.
    ''' 
    Public Event Uninitilized()
    
    Sub New()
        Try
            If Not Initialized Then
                AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveAppDomainAssemblies
                Initialized = True
                RaiseEvent Initilized()
            End If
        Catch ex As Exception
            'Maybe some error logging in the future.
            MsgBox(ex.Message)
        End Try
    End Sub
    
    #Region "IDisposable Support"
    Private disposedValue As Boolean ' To detect redundant calls
    
    ' IDisposable
    Protected Overridable Sub Dispose(disposing As Boolean)
        If Not disposedValue Then
            If disposing Then
                RemoveHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf ResolveAppDomainAssemblies
                _Initialized = False
                RaiseEvent Uninitilized()
            End If
        End If
        disposedValue = True
    End Sub
    
    ' This code added by Visual Basic to correctly implement the disposable pattern.
    Public Sub Dispose() Implements IDisposable.Dispose
        ' Do not change this code.  Put cleanup code in Dispose(disposing As Boolean) above.
        Dispose(True)
    End Sub
    #End Region
    End Class
    
    Public Module EmbeddedAssemblyResolverModule
    
    ''' 
    ''' Returns a dictionary of assemblies loaded in the current AppDomain by full name as key.
    ''' 
    ''' [Dictionary(Of String, Assembly)]
    Public ReadOnly Property AppDomainAssemblies As Dictionary(Of String, Assembly)
        Get
            Return AppDomain.CurrentDomain.GetAssemblies.ToDictionary(Function(a) a.FullName)
        End Get
    End Property
    
    ''' 
    ''' Method that attempts to resolve assemblies already loaded to the current AppDomain.
    ''' 
    ''' [Object]
    ''' [ResolveEventArgs]
    ''' [Assembly]
    Public Function ResolveAppDomainAssemblies(sender As Object, args As ResolveEventArgs) As Assembly
        'Return the existing assembly if it has already been loaded into the current AppDomain.
        If AppDomainAssemblies.ContainsKey(args.Name) Then Return AppDomainAssemblies.Item(args.Name)
        'Build the potential embedded resource name.
        Dim ResourceName As String = String.Format("{0}.{1}.dll", Assembly.GetExecutingAssembly().FullName.Split(",").First, args.Name.Split(",").First)
        'Attempt to load the requested assembly from the current assembly's embedded resources.
        Return Assembly.GetExecutingAssembly.LoadEmbeddedAssembly(ResourceName)
    End Function
    
    ''' 
    ''' Loads an assembly from the current assembly's embedded resources.
    ''' 
    ''' [Assembly] Current assembly which contains the embedded assembly.
    ''' [String] Full name of the embedded assembly.
    ''' [Assembly]
    
    Public Function LoadEmbeddedAssembly(CurrentAssembly As Assembly, EmbeddedAssemblyName As String) As Assembly
        'Return the existing assembly if it has already been loaded into the current AppDomain.
        If AppDomainAssemblies.ContainsKey(EmbeddedAssemblyName) Then Return AppDomainAssemblies.Item(EmbeddedAssemblyName)
        'Attempt to load the requested assembly from the current assembly's embedded resources.
        Using Stream = CurrentAssembly.GetManifestResourceStream(EmbeddedAssemblyName)
            If Stream Is Nothing Then Return Nothing
            Dim RawAssembly As [Byte]() = New [Byte](Stream.Length - 1) {}
            Stream.Read(RawAssembly, 0, RawAssembly.Length)
            Return Assembly.Load(RawAssembly)
        End Using
    End Function
    End Module
    

    The EmbeddedAssemblyResolverClass is used to create the actual AssemblyResolve event handler. I added a few bells and whistles by adding IDisposable support and events for Initialized and Uninitialized, but you can trim those off if not desired.

    I created the rest of the code in the EmbeddedAssemblyResolverModule so that they would be global to my assembly and also because the LoadEmbeddedAssembly method is an Extension method, of which can only be created in Modules.

    Now the only thing left to do is to create and instantiate the EmbeddedAssemblyResolverClass in any other class in your application that uses an assembly that is embedded into its resources.

    '''' 
    '''' Used to auto initialize the EmbeddedAssemblyResolverClass.
    '''' 
    Public WithEvents EAR As New EmbeddedAssemblyResolverClass
    

    Once you call a method from an embedded resource it will first look to see if the assembly is already loaded in the current AppDomain, if it is then the assembly is returned. If the embedded assembly has not been loaded then it will be loaded dynamically from the embedded resources if it exists.

    One thing that is nice about this code is that it works on assemblies that don't have an EntryPoint, like class libraries. Also I have had success in loading embedded assemblies with embedded assemblies which also used this code.

提交回复
热议问题