Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Definiting the Serialization DataMember in Interface and then using it in a class implementing said interface

Basically, should the code below work and serialize string Yoyo when an object of type YoyoData is returned over the wire.

    public interface IHelloV1
    {
        #region Instance Properties

        [DataMember(Name = "Yoyo")]
        string Yoyo { get; set; }

        #endregion
    }


    [DataContract(Name = "YoyoData", Namespace = "http://hello.com/1/IHelloV1")]
    public class YoyoData : IHelloV1
    {
        string Yoyo { get; set; }

        public YoyoData()
        {
            Yoyo = "whatever";
        }
    }
}
like image 533
Matt Avatar asked Jan 16 '23 22:01

Matt


1 Answers

I don't think it will.

The DataMember attribute is not inherited in derived classes.

For more details see documentation of type DataMemberAttribute and how it is defined : http://msdn.microsoft.com/en-us/library/system.runtime.serialization.datamemberattribute.aspx. This attribute specifies the property Inherited = false meaning the attribute won't be propagated to derived classes.

Also see http://msdn.microsoft.com/en-us/library/84c42s56(v=vs.71).aspx for more details concerning the Inherited property on attributes.

Anyway, this means that in your class defining the DataContract, the property Yoyo won't be considered a DataMember so for me it will just not work as expected.

like image 137
darkey Avatar answered Jan 19 '23 01:01

darkey