winforms icon indicating copy to clipboard operation
winforms copied to clipboard

Type 'System.Drawing.Printing.Margins' is not marked as serializable

Open alepikhov opened this issue 7 years ago • 5 comments

  • .NET Core Version: 3.0.100-rc1-014183
  • Have you experienced this same bug with .NET Framework?: No

While execute follow piece of code we faced with SerializationException.

System.Runtime.Serialization.SerializationException: 'Type 'System.Drawing.Printing.Margins' in Assembly 'System.Drawing.Common, Version=4.0.1.0, Culture=neutral, PublicKeyToken=cc7b13ffcd2ddd51' is not marked as serializable.'

Margins margins = new Margins(100, 100, 100, 100);
Margins margins2 = null;
using (Stream s = new MemoryStream())
{
	BinaryFormatter b = new BinaryFormatter();
	b.Serialize(s, margins);
	
	s.Seek(0, SeekOrigin.Begin);
	margins2 = (Margins)b.Deserialize(s);

}

But according with documentation topic Magrins class should has [System.Serializable] attribute.

https://docs.microsoft.com/en-us/dotnet/api/system.drawing.printing.margins?view=netcore-3.0

alepikhov avatar Sep 09 '19 15:09 alepikhov

This is a regression in CoreFx. In https://github.com/dotnet/corefx/commit/844415759d3f1525bf1f5d1bd79453c6aa8493f8 System.Drawing.Printing.Margins no longer marked as serialisable.

@stephentoub @ericstj please advise.

RussKie avatar Sep 09 '19 16:09 RussKie

@alepikhov do you have a usecase besides plain BinaryFormatter serialization? This kind of serialization is phased out and it is probably not a reason to bring back serialization of this type just for the sake of it. Serialization has been removed from plenty of types in .NET Core and without a concrete usecase of why its needed on this type its unlikely that it will be made serializable again.

weltkante avatar Sep 09 '19 17:09 weltkante

[Serializable] is only applied to ~200 types in .NET Core. This serialization mechanism has had serious maintenance and security concerns in the past. Please see https://docs.microsoft.com/en-us/dotnet/standard/serialization/binary-serialization for the full list.

I'll open a docs bug to make this clearer in individual topics.

danmoseley avatar Sep 09 '19 17:09 danmoseley

Hi @weltkante , @danmosemsft.

@alepikhov and I are from DevExpress and we're still working on adopting our Reporting Tools to .NET Core 3. DevExpress Reporting uses the System.Drawing.Printing.Margins class to store page settings and puts these settings into localized resource files (Satellite Resource Assemblies). Many of our customers rely on this functionality and can be faced with an unexpected runtime exception (SerializationException).

Do you guys have some advice or we'd better come up with our own serializable Margins class alternative?

e1em3ntoDX avatar Sep 10 '19 09:09 e1em3ntoDX

/cc: @ericstj

@danmosemsft do you think we should move the issue to the CoreFx repo?

RussKie avatar Sep 10 '19 11:09 RussKie