【发布时间】:2019-12-10 03:10:40
【问题描述】:
我有一个 .net 标准库,它会引发 netstandard2.0 (System.Configuration.ConfigurationErrorsException) 中定义的异常。我有一个针对调用标准库的 4.7.2 框架编译的测试程序集,并且想要捕获异常。一切都编译得很好。在运行时,catch 不会触发,因为它链接到的 ConfigurationErrorsException 来自 4.7.2 而不是标准。请注意我从单元测试中得到的错误。
如何告诉我的框架代码捕获标准异常?注意我也不想/不能将测试程序集变成网络标准。
消息:测试方法RequiredFieldNotSet 抛出异常 System.Configuration.ConfigurationErrorsException,但预期会出现异常 System.Configuration.ConfigurationErrorsException。异常消息:System.Configuration.ConfigurationErrorsException:需要参数“AnInt”但未设置!
try
{
SampleSettingsClass target = new SampleSettingsClass(settingsProvider);
}
catch (System.Configuration.ConfigurationErrorsException)
{
failed = true;
}
如果我改用 catch(Exception),则会捕获异常。预期框架异常和抛出的网络标准异常的完全限定类型不同。
//framework
typeof(System.Configuration.ConfigurationErrorsException).AssemblyQualifiedName
"System.Configuration.ConfigurationErrorsException, System.Configuration, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a"
//net standard
e.GetType().AssemblyQualifiedName
"System.Configuration.ConfigurationErrorsException, System.Configuration.ConfigurationManager, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51"
【问题讨论】:
-
我很困惑,因为我在list of .NET Standard APIs 中看不到 System.Configuration。它也没有在 .NET Standard 2.0 的 .NET API Browser 中列出。
-
@Powerlord 也许你想要这个source.dot.net/#System.Configuration.ConfigurationManager/…
-
@PranavSingh 这来自 .NET Core 源代码,而不是 .NET Standard。
-
为什么会编译呢?
-
反汇编指向system.configuration.configurationmanager\4.5.0\ref\netstandard2.0\System.Configuration.ConfigurationManager.dll
标签: c# .net-core .net-standard