Windows 带有用户控件的表单工具箱 System.TypeInitializationException

Windows Forms Toolbox System.TypeInitializationException with UserControls

我正在使用 VS2015 处理一个使用 Windows 表单的用户控件的项目。 上次我使用 Controls 时一切正常,完全没有显示错误。但是重启VS后工具箱无法显示控件。

当我开始将控件添加到其他表单时,它显示错误:

Error while creating the component ctrlObjectEditor.

Error message: System.TypeInitializationException

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

... (only rough translation)

当我 运行 Programm 时它工作正常但我不能在工具箱中编辑它。

控件都在同一个程序集中。 我发现了其他问题,但它们似乎对我不起作用。 (例如 AnyCPU、x64、x86、从控件中删除 Logger)

有人知道哪里出了问题吗?

看来我自己找到了答案

Class FileLogger 有一个静态参数填充值:

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

设计者 运行 时,此方法似乎会导致异常,因此我将其替换为:

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

这似乎有效。