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
URL:
Depends on:
Blocks:
 
Reported: 2013-07-11 09:29 UTC by foka
Modified: 2013-08-27 05:46 UTC (History)
3 users (show)

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


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


Notice (2018-05-24): bugzilla.xamarin.com is now in read-only mode.

Please join us on Visual Studio Developer Community and in the Xamarin and Mono organizations on GitHub to continue tracking issues. Bugzilla will remain available for reference in read-only mode. We will continue to work on open Bugzilla bugs, copy them to the new locations as needed for follow-up, and add the new items under Related Links.

Our sincere thanks to everyone who has contributed on this bug tracker over the years. Thanks also for your understanding as we make these adjustments and improvements for the future.


Please create a new report for Bug 13173 on GitHub or Developer Community if you have new information to add and do not yet see a matching new report.

If the latest results still closely match this report, you can use the original description:

  • Export the original title and description: GitHub Markdown or Developer Community HTML
  • Copy the title and description into the new report. Adjust them to be up-to-date if needed.
  • Add your new information.

In special cases on GitHub you might also want the comments: GitHub Markdown with public comments

Related Links:
Status:
NEW

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>
</Foo>


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.
eg:


[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>
</Bar>