这个问题看起来太菜鸟了,但我无法在15分钟内用Google回答。

我正在尝试为.net Standard创建类​​库并陷入异常定义。

我正在使用众所周知的Exception片段,它会生成以下代码:

    [Serializable]
    public class MyException : Exception
    {
        public MyException() { }
        public MyException(string message) : base(message) { }
        public MyException(string message, Exception inner) : base(message, inner) { }
        protected MyException(
          SerializationInfo info,
          StreamingContext context) : base(info, context) { }
    }


但是,在编译过程中出现错误:

1>Exceptions\MyException.cs(2,22,2,35): error CS0234: The type or namespace name 'Serialization' does not exist in the namespace 'System.Runtime' (are you missing an assembly reference?)
1>Exceptions\MyException.cs(7,6,7,18): error CS0246: The type or namespace name 'SerializableAttribute' could not be found (are you missing a using directive or an assembly reference?)
1>Exceptions\MyException.cs(7,6,7,18): error CS0246: The type or namespace name 'Serializable' could not be found (are you missing a using directive or an assembly reference?)
1>Exceptions\MyException.cs(14,11,14,28): error CS0246: The type or namespace name 'SerializationInfo' could not be found (are you missing a using directive or an assembly reference?)
1>Exceptions\MyException.cs(15,11,15,27): error CS0246: The type or namespace name 'StreamingContext' could not be found (are you missing a using directive or an assembly reference?)


根据this page,dotnet标准具有Seri​​alizationAttribute。
我是否需要安装特定于目标dotnet标准1.2的软件?

最佳答案

SerializableAttributeincluded in .NET Standard 2.0,而不是1.2。

关于c# - 使用.NET Standard声明C#异常,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/44040149/

10-11 02:19