Saturday, November 15, 2008

Array Serialization

For the private array variable I tried writing the indexed property:

  public float this [int index]

   {

     get

        {

          return m_Price[index];

         }

     set

        {

          m_Price[index] = value;

        }

   }

Still this is not sufficient. The get/set proerty must be written as:

        public float[] Price

        {

            get

            {

                return m_Price;

            }

            set

            {

                m_Price = value;

            }

        }

Interestingly, the MSDN document for Serializable Attribute states that: “All the public and private fields in a type that are marked by the SerializableAttribute are serialized by default, unless the type implements the ISerializable interface to override the serialization process.”

More about SerializableAttribute on: http://msdn2.microsoft.com/en-us/library/system.serializableattribute(vs.80).aspx

If the attribute serializes all public, private fields then why get/set is required? L

While sending custom object from web service, the serialization of object could also be achieved by using “System.Xml.Serialization.XmlInclude” instead of “Serializable” attribute.

The code like

[System.Xml.Serialization.XmlInclude(typeof(MarketInformation. PriceInformation))];

should be added instead of [Serializable] attribute.

 

If you don’t have access to the class code base then it could be added on top of the web method using it. Like following:

 

[WebMethod]

[System.Xml.Serialization.XmlInclude(typeof(MarketInformation. PriceInformation))];

public PriceInformation GetPriceInfo()

    {

        PriceInformation price = new PriceInformation ();

        return price;

    }

The member variables will require get and set proerties same as in case of Serializable attribute.

No comments: