Sindorej
Sindorej

Reputation: 181

C#: SerializableAttribute could not be found

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

Upvotes: 4

Views: 7016

Answers (2)

bartonjs
bartonjs

Reputation: 33098

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).

Upvotes: 3

Christian Gollhardt
Christian Gollhardt

Reputation: 17014

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.

Upvotes: 3

Related Questions