.NET Core - 找不到类型或命名空间名称“SystemException”

.NET Core - The type or namespace name ‘SystemException’ could not be found

我在 .NET Core 1.0 class 库中遇到以下编译错误:

“The type or namespace name ‘SystemException’ could not be found (are you missing a using directive or an assembly reference?)”, code: CS0246.

project.json

 {
  "name": "Dna.Net.Core",
  "version": "1.0.0-*",

  "dependencies": {
    "Autofac": "4.1.0",
    "NETStandard.Library": "1.6.0",
    "System.Data.SqlClient": "4.1.0",
    "System.Runtime": "4.1.0",
    "System.Runtime.Serialization.Formatters": "4.0.0-rc3-24212-01",
    "System.Runtime.Serialization.Primitives": "4.1.1"
  },

  "frameworks": {
    "netstandard1.6": {
      "imports": "dnxcore50"
    }
  }
}

ISystemExceptionMessageBuilder.cs

using Dna.Net.Core.Common;
using System;

namespace Dna.Net.Core.Exceptions
{
    public partial interface ISystemExceptionMessageBuilder
    {
        CustomMessage Parse(SystemException ex);
    }
}

Framework Guideline 声明“不要抛出异常或 SystemException”。

coreFX 项目中的搜索显示 classes that have refactored away from System.SystemException 中的注释。

我可以/将/应该能够在 .NET Core 中捕获 System.SystemException 吗?

System.SystemException 存在于 coreCLR 中 - https://github.com/dotnet/coreclr/blob/master/src/mscorlib/src/System/SystemException.cs

因此它在 mscorlib 中可用。所以可以使用。但不推荐。因为当您抛出系统异常而不是抛出相关异常时,您不会获得有关异常的任何特定信息。系统异常应仅限于系统错误(特定于平台)。

System.SystemException 不是 .NET Core 的一部分(至少不是 netcoreapp1.0netstandard1.6)。然而,它 seems 安排在 netcoreapp1.1netstandard1.7

另一个答案的引用源文件可能没有编译到 System.Runtime(或其同伴 System.Private.CoreLib

回答您的问题:截至今天,您应该无法捕捉到 System.SystemException。对于最终处理,您应该捕获 System.Exception 并且如果您能够处理异常,则它不能是 .NET Core 中的 System.SystemException