【问题标题】:C#: SerializableAttribute could not be foundC#:找不到 SerializableAttribute
【发布时间】:2017-06-12 23:13:50
【问题描述】:

我正在尝试在 C# 类库项目(最新的 .NET 版本)中使用 [Serializable] 属性,但它无法识别

据我所知,Serializable 它属于 System.Runtime.Serialization 系统的东西,但我已经使用它了,它仍然不起作用。

我在其他项目(Unity)中使用它,但在这里不起作用。有什么想法吗?

using System;
using System.Runtime;
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Collections.Generic;

namespace Model{
    [Serializable]
    public struct GameSettings{
        public int Players;
    }
}

提前谢谢你

编辑:格式 Edit2:错误截图

【问题讨论】:

标签: c# .net xml serialization .net-core


【解决方案1】:

https://apisof.net/catalog/System.SerializableAttribute 表明 [Serializable] 是在 .NET Core 1.0 和 1.1 以及 .NET Standard 1.6 的 System.Runtime.Serialization.Formatters 包中定义的。然后它转移到 .NET Core 2.0 的 System.Runtime 和 .NET Standard 2.0 的 netstandard 单体。

如果您只关心能够编译共享代码,您可以尝试添加包引用。但是What is the equivalent of [Serializable] in .NET Core ? (Conversion Projects) 已经回答说二进制序列化程序不在 .NET Core 1.x 中(不确定它的 2.0 状态...但由于 2.0 处于预览状态,您可以随时尝试)。

【讨论】:

    【解决方案2】:

    这个属性存在于System,而不是System.Runtime.Serialization

    namespace System
    {
      /// <summary>Indicates that a class can be serialized. This class cannot be inherited.</summary>
      [AttributeUsage(AttributeTargets.Class | AttributeTargets.Struct | AttributeTargets.Enum | AttributeTargets.Delegate, Inherited = false)]
      [ComVisible(true)]
      public sealed class SerializableAttribute : Attribute
      {
          //...
      }
    }
    

    你确定你引用了mscorlib.dllThis question 可能对你很感兴趣。

    【讨论】:

    • 如您所见,系统已被使用。我确实尝试在依赖项中显式添加 mscorlib.dll,但这会产生更多错误。
    • Christian 的回答似乎对 Unity 是正确的。至少,在全新安装 Unity 2018.3.8f1 和相应的 VisualStudio 包后,它对我有用。
    • 是的,问题后来变成了.net核心问题。
    猜你喜欢
    • 2015-06-27
    • 1970-01-01
    • 2019-05-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-06
    • 2011-08-07
    • 1970-01-01
    相关资源
    最近更新 更多