[Mono-list] using DataContractSerializer on Objects with read only fields

ptr2009 ptrajkumar at gmail.com
Wed Dec 2 16:48:44 EST 2009

hey all

  I am attaching a small sample illustrating my problem. I have a simple
Object that has few readonly
fields with DataMember. 

Under mono I get the following exception ?

Unhandled Exception:
System.Runtime.Serialization.InvalidDataContractException: DataMember field
System.String name must not be read-only.

The Program runs without errors under .NET 3.5. Is this a known issue ?



using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;
using System.IO;

namespace TestApp
    public class Person
        readonly public string name;

        readonly public Guid Id = Guid.Empty;

        public Person(string nameIn, Guid idIn)
            name = nameIn;
            Id = idIn;

        public override string ToString()
            return string.Format("name={0},id={1}", name, Id);

    class Program
        static void Main(string[] args)

            Person p1 = new Person("UserName", Guid.NewGuid());
            Console.WriteLine("Person 1 " + p1.ToString());
            MemoryStream memStream = new MemoryStream();
            DataContractSerializer ser =  new
            ser.WriteObject(memStream, p1);
            memStream.Seek(0, SeekOrigin.Begin);
            Person p2 = (Person) ser.ReadObject(memStream);
            Console.WriteLine("Person 2 " + p2.ToString());

View this message in context: http://old.nabble.com/using-DataContractSerializer-on-Objects-with-read-only-fields-tp26617064p26617064.html
Sent from the Mono - General mailing list archive at Nabble.com.

More information about the Mono-list mailing list