0

Seriosly, why? Why should we always implicitly write [Serializable]? It makes me to write ugly code like this:

[Serializable]
public class SerializableFont
{
    public string Name { get; set; }
    public int Size { get; set; }

    public SerializableFont() { }

    public SerializableFont(string name, int size)
    {
        Name = name;
        Size = size;
    }

    public static implicit operator Font(SerializableFont font)
    {
        return new Font(font.Name, font.Size);
    }
}

or this

[Serializable]
public class SerializableColor
{
    public string ColorHtml { get; set; }

    public SerializableColor() { }

    public static implicit operator Color(SerializableColor color)
    {
        return ColorTranslator.FromHtml(color.ColorHtml);
    }

    public static implicit operator SerializableColor(Color color)
    {
        return new SerializableColor { ColorHtml = ColorTranslator.ToHtml(color) };
    }
}

or even when I want to run some code with closure in domain:

var domain = AppDomain.CreateDomain("DomainName");
string message = "Hello world!";
domain.DoCallBack(() => Console.WriteLine(message));

I get an exception

Unhandled Exception: System.Runtime.Serialization.SerializationException: Type 'MainProject.Program+<>c__DisplayClass1' in assembly 'LoadUnloadTest, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' is not marked as serializable.

All this issues could be easily avoided if all these types was [Serializable] by default.

Alex Zhukovskiy
  • 9,565
  • 11
  • 75
  • 151
  • 1
    And if we do Deserialize(Serialize(you)) we'll get two perfect clones :)... Most objects are just not serializable (neither in real life, nor in C#/.Net) as discussed in earlier [question](http://stackoverflow.com/questions/2982376/why-is-serializable-attribute-required-for-an-object-to-be-serialized) on this topic. – Alexei Levenkov Oct 15 '15 at 16:03
  • Because that would be a bad idea? And, in your example, that would allow pollution of other appdomains trivially easy, thus making them practically useless as a tool for code isolation. –  Oct 15 '15 at 16:05

0 Answers0