C# Serialization Guide Part 5 – functions

23 בApril 2014

אין תגובות

In general functions is out of the scope. There is no reason to serialize functions, and if you exist you can serialize text who describe the function logic, we need to remember that the all idea of

.seilalization is to transfer data from endpoint to endpoint

In WCF we did something close but yet, we are just exposing an public service signature and not the implementation, the reason of this discussion came out since we are try to keep a whole

.object on a Text-file and maybe we like to deserialized it as is

.For a start lets exam the result of our serilazing process , its very easy in case of XML file

:Use this class  (or anything like it)

public class SerializeTest

    {

        private string prv = "abc";

        public string pbl = "def";


        public string func(string s)

        {

            return " hello " + s;

        }

    }

.Now try to serialize it by methods we exam at earlier demos in this series

,Of course we should point to XML file

So, basically

:Using Datacontract–Serializer

.Cannot mark function with DataMember attribute

:XML–Serializer

just Ignore the functions area and in case of delegate it throws Exception

.Well, delegate may sounds as best guess until this point

And it could work, but not like that ! so lets return to customize serialization using Iserializable

:interface

[Serializable]

  public class SerializeTest : ISerializable

  {

      public string Value;

      public SerializeDelegate del = new SerializeDelegate(() => { Console.WriteLine("from delegate !!"); });


      public SerializeTest()

      {

          Value = "i am serializble object";

      }


      public void GetObjectData(SerializationInfo info, StreamingContext context)

      {

          info.AddValue("ValueProperty", Value);

          info.AddValue("DelegeteProp", del);

      }


      public SerializeTest(SerializationInfo info, StreamingContext context)

      {

          Value = (string)info.GetValue("ValueProperty", typeof(string));

          del = (SerializeDelegate) info.GetValue("DelegeteProp",del.GetType());

      }

  }

:And serialize it as we demonstrate before

public class CustomSerializeService : ISerializeService

    {

        public bool write(object data, Type type, string path)

        {

            bool res = false;

            try

            {

                IFormatter formatter = new SoapFormatter();

                FileStream stream = new FileStream(path, FileMode.Create);

                formatter.Serialize(stream, data);

                stream.Close();

            }

            catch (Exception ex)

            {

                //deal with exception

            }

            return res;

        }


        public object read(string path, Type t)

        {

            IFormatter formatter = new SoapFormatter();

            FileStream s = new FileStream(path, FileMode.Open);

            return formatter.Deserialize(s);

        }

    }


Now it could work and after exam the output xml file it found a real object of type delegate was

.serialize include allot of metadata

.Again this is not so reasonable. Serialization is about Data

.Another proper solution is to think about Command Pattern as decorator for functions

And for the End. More professional implementation may found using Expression of .Net go

:here

 http://msdn.microsoft.com/en-us/library/bb397951.aspx

,For expression info

:And proposal solution of serialization is here

http://archive.msdn.microsoft.com/exprserialization

this is probably a very efficient way to serialize method or if to be correct an expression – .meaning a specific flow of code

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

Leave a Reply

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