Bug 13173 - DataContractSerializer serializes const and tries to set it on deserialize
Summary: DataContractSerializer serializes const and tries to set it on deserialize
Status: NEW
Alias: None
Product: Class Libraries
Classification: Mono
Component: WCF assemblies (show other bugs)
Version: master
Hardware: PC Linux
: --- normal
Target Milestone: Untriaged
Assignee: Bugzilla
Depends on:
Reported: 2013-07-11 09:29 UTC by foka
Modified: 2013-08-27 05:46 UTC (History)
3 users (show)

See Also:
Is this bug a regression?: ---
Last known good build:

Sample app (914 bytes, text/plain)
2013-07-11 09:29 UTC, foka

Description foka 2013-07-11 09:29:47 UTC
Created attachment 4315 [details]
Sample app

DataContractSerializer on linu mono serializes public constant field. On MS.NET it does not which is expected.
So on deserialize this const tries to be set, which ends with an exception:
System.InvalidOperationException: Failed to set value of type System.String for property System.String Bar ---> System.FieldAccessException: Cannot set a constant field
  at System.Reflection.MonoField.SetValue

Result of serialization of this class
public class Foo
	public const string Bar = "Wow. Bravo.";

on Ms.net:
<?xml version="1.0" encoding="utf-16"?>
<Foo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/SerializeConst" />

and on Linux mono:
<?xml version="1.0" encoding="utf-8"?>
<Foo xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/SerializeConst">
        <Bar>Wow. Bravo.</Bar>

Checked the attached app. Compile it with: mcs Program.cs -r:System.Runtime.Serialization
Comment 1 Mateusz Mor 2013-08-27 05:46:13 UTC
Similar problem with serialization occurs when we try to serialize class which is not marked with DataContract attribute, but derived from class with DataContract attribute.

In such case mono linux DataContractSerialize serializes public static readonly fields. It does not heppens on microsoft implementation.

[DataContract(IsReference = true)]
public class Foo{}

public class Bar : Foo
  public static readonly string Hello = "I am invisible";
Windows serialization:
<?xml version="1.0" encoding="utf-16"?>
<Bar xmlns:i="http://www.w3.org/2001/XMLSche
atacontract.org/2004/07/TestProjForMono" />

Linux serialization:
<?xml version="1.0" encoding="utf-16"?>
<Bar xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/IsReferenceTest">
  <Hello>I am invisible</Hello>

Note You need to log in before you can comment on or make changes to this bug.