TypeLoadException Help

I am writing a .NET 3.5 application (WinForms) that uses classes from an external DLL, and I always get it System.TypeLoadExceptionevery time the application tries to start.
Here is an example of a VS mapping:

System.TypeLoadException was unhandled
  Message = Could not load type 'PolyMorph.Common.Settings' from assembly 'PolyMorph, Version = 1.0.0.0, Culture = neutral, PublicKeyToken = null'.
  Source = PolyMorph
  TypeName = PolyMorph.Common.Settings
  Stacktrace:
       at PolyMorphApp.App.Initialize ()
       at PolyMorphApp.App.Main ()
       at System.AppDomain._nExecuteAssembly (Assembly assembly, String [] args)
       at System.AppDomain.ExecuteAssembly (String assemblyFile, Evidence assemblySecurity, String [] args)
       at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly ()
       at System.Threading.ThreadHelper.ThreadStart_Context (Object state)
       at System.Threading.ExecutionContext.Run (ExecutionContext executionContext, ContextCallback callback, Object state)
       at System.Threading.ThreadHelper.ThreadStart ()
  InnerException: 

Here is the code I'm running:

Friend NotInheritable Class App

    <STAThread()> Shared Sub Main()
        'set the exception handlers'
        Application.SetUnhandledExceptionMode(UnhandledExceptionMode.CatchException)
        AddHandler AppDomain.CurrentDomain.UnhandledException, AddressOf UnhandledExceptionHandler
        AddHandler Application.ThreadException, AddressOf ThreadExceptionHandler
        'initialize the application'
        App.Initialize()

        'and then run the application'
        Dim mainForm As New PolymorphHost
        Application.Run(mainForm)
    End Sub

    Shared Function Initialize() As FunctionResult
        If App.InitializeDataDirectory() = False Then
            Return New FunctionResult(False, "the application data directory")
        End If


        _settings = New PolyMorph.Common.Settings(AppDataDirectory & "\Settings.dat")
        ......code continues to load settings from the _settings variable
    End Function
End Class



It amazes me that the VS2010 Debugger stops on the line App.Initialize()without even entering a function Initialize.

If, however, I comment out all links to an external DLL in a function Initialize, the application initializes correctly.


, , , ( x64 DLL, x86). , DLL x86, TypeLoadException.

-, ?

+3
6

, Nutzy , , , . , .

, . , .

+2

InnerException LoadException, , .

, , , JIT. , CLR MSIL , . PolyMorph.Common.Settings Initialize, CLR . , .

, , try... catch Initialize.

Try
    InitializeInternal()
Catch ex As TypeLoadException
    System.Diagnostics.Debugger.WriteLine(ex.ToString())
End Try
+4

, . Initialize. , .

Debug (. System.Diagnostics) , , .

+1

, , VisualStudio JIT-. , DLL x64 vs x86, . - . - ( :)

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct Foo
    {
        int x;
    }

. , #if . #false, , , , TypeLoadException

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct uartparam_t
    { //size 112
        public UInt16 portnum; //0
        public Byte conntype;
        public Byte baud;
        public Byte databits;
        public Byte stopbits;
        public Byte parity;
        public Byte flowctrl;
#if true
        public int remoteip;
#else
        public Foo remoteip; //8 -- for some reason this is making the program crash! ?!?!?
#endif

     ....

    [StructLayout(LayoutKind.Sequential, Pack = 1)]
    public struct config {
 ...
        [MarshalAs(UnmanagedType.ByValArray, SizeConst = MAXUARTPORT)]
        public uartparam_t[] uartparam; //(size 1792)
 ...
    }

. , , . - int int Foo .

+1

, . , , , ​​ GAC, . , , , , GAC, . , .

0

My problem was in the parent assembly, and the DLL had the same assembly name.

eg. Application.exe and Application.dll

I changed each to a separate name in the project properties (e.g. Application.exe and Library.dll) and it fixed the problem. I think that they also rename the selected answer ("transition to a new solution").

0
source

Source: https://habr.com/ru/post/1782300/


All Articles