Advertisement
Guest User

Untitled

a guest
Jul 12th, 2017
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization;
  4.  
  5. namespace TestApp
  6. {
  7.     public interface ICloneable<out T>
  8.     {
  9.         T Clone();
  10.     }
  11.  
  12.     [DataContract(Name = "Test2_name", Namespace = "Test2_namespace")]
  13.     public class Test2 : ICloneable<Test2>
  14.     {
  15.         private string _message = string.Empty;
  16.  
  17.         [DataMember(Name = "Message")]
  18.         public string Message
  19.         {
  20.             get { return _message; }
  21.             set { _message = value; }
  22.         }
  23.  
  24.         public Test2 Clone()
  25.         {
  26.             var serializer = new DataContractSerializer(typeof(Test2), null, int.MaxValue, false, true, null);
  27.             using (var ms = new MemoryStream())
  28.             {
  29.                 serializer.WriteObject(ms, this);
  30.                 ms.Position = 0;
  31.                 return (Test2)serializer.ReadObject(ms);
  32.             }
  33.         }
  34.     }
  35.  
  36.     class Program
  37.     {
  38.         static void Main(string[] args)
  39.         {
  40.             Test2 test2 = new Test2();
  41.             test2.Message = "hello";
  42.             var s2 = test2.Clone();
  43.             Console.WriteLine(s2.Message);
  44.         }
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement