none
IExtensibleDataObject implementation

    Question

  • Every implementation example of IExtensibleDataObject I've seen looks just like...
        private ExtensionDataObject extensionData;
        public ExtensionDataObject ExtensionData
        {
            get
            {
                return extensionData;
            }
            set
            {
                extensionData = value;
            }
        }
    
    couldn't you just implement like this...

    public class A : IExtensibleDataObject
    {
         public ExtensionDataObject ExtensionData {get; set;}
    }

    is there some difference between the two?

    Wednesday, February 24, 2010 10:13 PM

Answers

  • That will also work; it's just that IExtensibleDataObject was first introduced (.Net 3.0) when the automatic property syntax didn't exist yet (IIRC it was introduced in .Net 3.5), which is why you see most examples with the property and the field declaration. The code below works just fine (with .Net 3.5 and above).

        public class Post_5aeff302_304a_4f42_bd90_4b2f6216ac04
        {
            [DataContract(Name = "MyType")]
            public class MyType_V1 : IExtensibleDataObject
            {
                public ExtensionDataObject ExtensionData { get; set; }
                [DataMember]
                public string str;
            }
            [DataContract(Name = "MyType")]
            public class MyType_V2 : IExtensibleDataObject
            {
                public ExtensionDataObject ExtensionData { get; set; }
                [DataMember]
                public string str;
                [DataMember]
                public string str2;
            }
            static string Serialize(object o)
            {
                MemoryStream ms = new MemoryStream();
                DataContractSerializer dcs = new DataContractSerializer(o.GetType());
                dcs.WriteObject(ms, o);
                return Encoding.UTF8.GetString(ms.ToArray());
            }
            static T Deserialize<T>(string serialized)
            {
                MemoryStream ms = new MemoryStream(Encoding.UTF8.GetBytes(serialized));
                DataContractSerializer dcs = new DataContractSerializer(typeof(T));
                return (T)dcs.ReadObject(ms);
            }
            public static void Test()
            {
                MyType_V2 v2 = new MyType_V2 { str = "Hello", str2 = "world" };
                string ser1 = Serialize(v2);
                Console.WriteLine(ser1);
                Console.WriteLine();
                MyType_V1 v1 = Deserialize<MyType_V1>(ser1);
                Console.WriteLine(v1);
                Console.WriteLine();
                string ser2 = Serialize(v1);
                Console.WriteLine(ser2);
                Console.WriteLine();
                v2 = Deserialize<MyType_V2>(ser2);
                string ser3 = Serialize(v2);
                Console.WriteLine(ser3);
                Console.WriteLine();
                Console.WriteLine(ser1 == ser3);
            }
        }
    
    • Marked as answer by Bin-ze Zhao Monday, March 01, 2010 10:45 AM
    Thursday, February 25, 2010 5:41 AM