Search code examples
c#winformsuser-controlstypeinitializationexception

Windows Forms Toolbox System.TypeInitializationException with UserControls


I am using VS2015 to work on a Project using UserControls for Windows Forms. The last time I used the Controls everything worked fine and it had no display errors at all. However after restarting VS the Toolbox was unable to display the Controls.

When I start to add the Controls to other Forms it displays the error:

Error while creating the component ctrlObjectEditor.

Error message: System.TypeInitializationException

The Typeinitializer for "project.Log.LoggerHost" threw an Exception.

... (only rough translation)

When I run the Programm it works fine but I can not edit this in the Toolbox.

The Controls are all in the same Assembly. I have found other Questions but they dont seem to work for me. (e.g. AnyCPU, x64, x86, removing Logger from Controls)

Does anyone have an idea what could be wrong?


Solution

  • It seems I found the answer myself.

    The Class FileLogger had a static Parameter filled with the value:

    Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)
    

    It seems this Method causes an Exception when run by the designer so I replaced it with:

    try
    {
        var dir = Path.GetDirectoryName(Assembly.GetEntryAssembly()?.Location);
        return Path.Combine(dir, "Logs");
    }
    catch (ArgumentException)
    {
        return "C:\\Logs";
    }
    

    And this seems to work.