4

I am trying to use the [Serializable] attribute in a C# class library project (latest .NET version), but it is not recognised.

As far as I could search, Serializable it something that belongs in System.Runtime.Serialization System, but I have used it and it still doesn't work.

I am using it in other projects (Unity), but it doesn't work here. Any ideas?

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;
    }
}

The exact error I am getting

Thank you in advance

Edit: format Edit2: screenshot of the error

dbc
  • 104,963
  • 20
  • 228
  • 340
Sindorej
  • 181
  • 2
  • 12
  • What is your environment? .Net 4.5? .Net core? Unity3d? Something else? – dbc Jun 11 '17 at 21:03
  • what is your problem exactly? – amin Jun 11 '17 at 21:10
  • @dbc I am using the latest .NET version for a .NETCoreApp v1.1 – Sindorej Jun 11 '17 at 22:27
  • @dbc The [Serializable] attribute does work in Unity, but that is a standalone class library (as I mentioned above) – Sindorej Jun 11 '17 at 22:27
  • Have you disabled the core libraries in your project (e.g. https://stackoverflow.com/a/35902373/34092 or https://stackoverflow.com/a/11560352/34092 )? – mjwills Jun 11 '17 at 22:37
  • Define 'doesn't work'? Do you mean 'it doesn't compile' or something else? If something else, talk us through what that looks like. – mjwills Jun 11 '17 at 22:38
  • This is the error I am getting. [link](http://prntscr.com/finarm) I'm checking out the linked questions now – Sindorej Jun 11 '17 at 22:41
  • @mjwills I checked the links you have provided and no; I have not done anything to remove mscorelib from the references – Sindorej Jun 11 '17 at 22:44
  • 4
    Possible duplicate of [What is the equivalent of \[Serializable\] in .NET Core ? (Conversion Projects)](https://stackoverflow.com/questions/39199049/what-is-the-equivalent-of-serializable-in-net-core-conversion-projects) – mjwills Jun 11 '17 at 22:51

2 Answers2

3

This attribute lives in System, not in 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
  {
      //...
  }
}

Are you sure you have referenced mscorlib.dll? This question might be interessting for you.

Christian Gollhardt
  • 16,510
  • 17
  • 74
  • 111
3

https://apisof.net/catalog/System.SerializableAttribute shows that [Serializable] is defined in the System.Runtime.Serialization.Formatters package for .NET Core 1.0 and 1.1 and .NET Standard 1.6. It then moved to System.Runtime for .NET Core 2.0 and the netstandard monolith for .NET Standard 2.0.

If you just care about being able to compile shared code, you could try adding the package reference. But What is the equivalent of [Serializable] in .NET Core ? (Conversion Projects) has already answered that the binary serializer isn't in .NET Core 1.x (unsure about its 2.0 status... but since 2.0 is in preview you could always try it out).

bartonjs
  • 30,352
  • 2
  • 71
  • 111