C# Serialization Guide Part 7 – Known Types

2 בMay 2014

אין תגובות

A special attribute that can be added to any serialized object (where using DataContractSerialization)

There is a few ways to demonstrate the knownType usage, i myself check out a few examples

..around the net and they where kind of so so

,In my example we will try to put inside a serialized object a referenced member of type Object

.meaning not a final implementation

.it is relevant when Either you writing a base service or not yet implement the reference object

:The code structure may be like that

[DataContract]

 public class MyData

 {

     [DataMember]

     public int PublicID { get; set; }


     [DataMember]

     private int PrivateID = 9999;



     [DataMember]

     public object referenceData { get; set; }



     public MyData()

     {


     }


  }

:And initialize this object will be like that

[DataContract]

    public class InternalData

    {

        public InternalData()

        {

        }


        [DataMember]

        public string internalValue;

    }


  MyData dat = new MyData()

            {

                PublicID = 123,

                referenceData = new InternalData() { internalValue = "test" }

            };

.Lets pointing to the fact that the referenceData now refer to InternalData class

.In this stage serializing the Data might work, but there will be no option to deserialize it

.Our job is to help the dataContract serializer to deserialize the xml from the stream

:And here came the KnownType attribute, we must use it like that

[KnownType(typeof(InternalData))]

And its should be used twice at least, on the InternalData object and on the MyData object

The idea is some meta-data on our types, to assistant deserializing process because object with

.none specific type cannot be translate itself directly

:So, full code will be as this one

[KnownType(typeof(InternalData))]

    [DataContract]

    public class MyData

    {

        [DataMember]

        public int PublicID { get; set; }


        [DataMember]

        private int PrivateID = 9999;



        [DataMember]

        public object referenceData { get; set; }


        public MyData()

        {


        }


    }



    [KnownType(typeof(InternalData))]

    [DataContract]

    public class InternalData

 {

        public InternalData()

        {

        }


        [DataMember]

        public string internalValue;

    }

Well, off course it is not very smart to hold typed object member in your class but as a demo it is

.good enough

But what about base class which is sounds much more better than just an object? well , come on

:lets try the use a base prototype

[KnownType(typeof(InternalDataBase))]

    [DataContract]

    public class MyData

    {

        [DataMember]

        public int PublicID { get; set; }


        [DataMember]

        private int PrivateID = 9999;



        [DataMember]

        public InternalDataBase referenceData { get; set; }



        public MyData()

        {


        }


        [OnSerialized]

    }



    [KnownType(typeof(InternalDataBase))]

    [DataContract]

    public class InternalData : InternalDataBase

    {

        public InternalData()

        {

        }


        [DataMember]

        public string internalValue;

    }



    [KnownType(typeof(InternalData))]

    [DataContract]

    public class InternalDataBase

    {

    }

,Now its much more right to extend the object

But this is a little confusing to put on the base class the known type of the implemented type !

Noticed that the known type of the InternalDataBase is the inherited class (InternalData)

And we should adding this line to any implementation, because if we will looking from the base

.class, again the reflection serialize process should know to the using type

.So in case of inheritance the attribute need to be on both sides with the other type

For the End if we are talking about Web services (WCF) using DataContract, its possiblly not the smartest thing to do letting the clients to be familiar with your internal object signature. so try to

..avoid it

הוסף תגובה
facebook linkedin twitter email

Leave a Reply

Your email address will not be published. Required fields are marked *