在框架中捕获.Net标准异常

问题描述 投票:0回答:1

我有一个.net标准库,该库引发了netstandard2.0(System.Configuration.ConfigurationErrorsException)中定义的异常。我有一个针对4.7.2框架编译的测试程序集,该程序集调用标准库,并希望捕获异常。一切都编译良好。在运行时,捕获不会触发,因为它链接到的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"
c# .net-core .net-standard
1个回答
0
投票

正如汉斯在评论中所说,如果我的框架应用链接到标准的System.Configuration程序集而不是框架版本,那么我可以捕获此异常。只是我期望的魔术重新输入不适用于(第三方)第三方库。

© www.soinside.com 2019 - 2024. All rights reserved.