Search code examples
vb.netactivexsystem.reflection

ActiveX Assembly ReflectionTypeLoadException


I created an activeX exe (registered with regasm.exe) that loads a DLL with

Dim a As System.Reflection.Assembly = System.Reflection.Assembly.LoadFile(FullPath)

This DLL works with an Interface "PSInterface". As normal exe it works, but when i call that activex object i am getting a ReflectionTypeLoadException in a.GetTypes. When i look into that error i get the following:

Could not load file or assembly 'PSInterface, Version=1.0.0.0, Culture = neutral, PublicKeyToken=null' or one of its dependencies. The System cannot find the file specified.

The PSInterface.dll is in the same folder as the exe and dll.

What can i do?


Solution

  • To load the Assemblies from the same folder, I set AddHandler AppDomain.CurrentDomain.AssemblyResolve, AddressOf LoadFromSameFolder and the Function

    Private Shared Function LoadFromSameFolder(ByVal sender As Object, ByVal args As ResolveEventArgs) As Assembly
            Dim folderPath As String = Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly.Location)
            Dim assemblyPath As String = Path.Combine(folderPath, New AssemblyName(args.Name).Name & ".dll")
            If Not IO.File.Exists(assemblyPath) Then
                assemblyPath = Path.Combine(folderPath, New AssemblyName(args.Name).Name & ".exe")
                If Not IO.File.Exists(assemblyPath) Then
                    Return Nothing
                End If
            End If
            Dim assembly As Assembly = Assembly.LoadFrom(assemblyPath)
            Return assembly
        End Function
    

    Thanks @Craig for your help.

    More Here: How to add folder to assembly search path at runtime in .NET?