Advertisement
Guest User

Dynamic serialization proposal

a guest
Jul 3rd, 2015
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.66 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. using System.IO;
  8.  
  9. using System.Xml.Linq;
  10. using System.Reflection;
  11. using System.Runtime.Serialization;
  12. namespace XmlSerializeTest
  13. {
  14.     public class DataClass
  15.     {
  16.         public DataClass Clone()
  17.         {
  18.             return (DataClass) this.MemberwiseClone();
  19.         }
  20.     }
  21.     [DataContract]
  22.     public class Foo : DataClass
  23.     {
  24.         private string test1 = "";
  25.         private string test2 = "";
  26.         [DataMember]
  27.         public string Test1 { get { return test1; } set { test1 = value; } }
  28.         [DataMember]
  29.         public string Test2 { get { return test2; } set { test2 = value; } }
  30.  
  31.     }
  32.     public class MeSerializer
  33.     {
  34.         Type targetType;
  35.         public MeSerializer(Type targetType)
  36.         {
  37.             this.targetType = targetType;
  38.             if (!targetType.IsDefined(typeof(DataContractAttribute), false))
  39.                 throw new Exception("No soup for you.");
  40.         }
  41.  
  42.         public void WriteObjectDiff(Stream stream, object changed, object orig)
  43.         {
  44.             IEnumerable<PropertyInfo> properties =
  45.                 targetType.GetProperties().Where(p => p.IsDefined(typeof(DataMemberAttribute), false));
  46.  
  47.             var writer = new StreamWriter(stream);
  48.             writer.Write("<" + targetType.Name + ">");
  49.  
  50.             foreach (PropertyInfo propInfo in properties)
  51.             {
  52.                 string s_orig = propInfo.GetValue(orig, null).ToString();
  53.                 if (orig != null)
  54.                 {
  55.                     string s_changed = propInfo.GetValue(changed, null).ToString();
  56.                     if (s_orig.Equals(s_changed)) continue;
  57.                 }
  58.                 writer.Write("<" + propInfo.Name + ">" + propInfo.GetValue(orig, null) + "</" + propInfo.Name + ">");
  59.             }
  60.             writer.Write("</" + targetType.Name + ">");
  61.             writer.Flush();
  62.         }
  63.     }
  64.     public class Program
  65.     {
  66.         static void Main(string[] args)
  67.         {
  68.             Foo foo = new Foo();
  69.             foo.Test1 = "bar";
  70.             foo.Test2 = "ASDF";
  71.             Foo foo2 = (Foo) foo.Clone();
  72.             foo.Test2 = "ASDG";
  73.  
  74.             MeSerializer ser = new MeSerializer(foo.GetType());
  75.  
  76.             var output = new MemoryStream();
  77.             ser.WriteObjectDiff(output, foo2, foo);
  78.             output.Seek(0, SeekOrigin.Begin);
  79.             string s = Convert.ToString(XElement.Parse(Encoding.ASCII.GetString(output.GetBuffer()).Replace("\0", "")));
  80.             Console.WriteLine(s);
  81.             Console.ReadKey();
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement