Guest User

Untitled

a guest
Oct 18th, 2021
473
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.65 KB | None | 0 0
  1.    
  2.     public static class SerializeExtensions
  3.     {
  4.         /// <summary>
  5.         /// Odtwarza dowolny obiekt ze serializowanego stringa xml
  6.         /// </summary>
  7.         /// <typeparam name="T"></typeparam>
  8.         /// <param name="obj"></param>
  9.         /// <returns></returns>
  10.         public static string SerializeToXmlString<T>(this T obj)
  11.         {
  12.             XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
  13.  
  14.             using (StringWriter textWriter = new StringWriter())
  15.             {
  16.                 xmlSerializer.Serialize(textWriter, obj);
  17.                 return textWriter.ToString();
  18.             }
  19.  
  20.         }
  21.  
  22.         /// <summary>
  23.         /// Dserializuje dowolny obiekt ze stringa xml
  24.         /// </summary>
  25.         /// <typeparam name="T"></typeparam>
  26.         /// <param name="obj"></param>
  27.         /// <param name="s"></param>
  28.         /// <returns></returns>
  29.         public static T DeserializeFromXmlString<T>(this T obj, string s)
  30.         {
  31.             var serializer = new XmlSerializer(obj.GetType());
  32.             T result;
  33.  
  34.             using (TextReader reader = new StringReader(s))
  35.             {
  36.                 result = (T)serializer.Deserialize(reader);
  37.             }
  38.  
  39.             obj = result;
  40.  
  41.             return result;
  42.         }
  43.  
  44.         public static string SerializeToXmlStringDc(this object obj)
  45.         {
  46.             using (MemoryStream memoryStream = new MemoryStream())
  47.             using (StreamReader reader = new StreamReader(memoryStream))
  48.             {
  49.                 DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
  50.                 serializer.WriteObject(memoryStream, obj);
  51.                 memoryStream.Position = 0;
  52.                 return reader.ReadToEnd();
  53.             }
  54.         }
  55.  
  56.         public static object DeserializeFromXmlStringDc(this string xml, Type toType)
  57.         {
  58.             using (Stream stream = new MemoryStream())
  59.             {
  60.                 byte[] data = Encoding.UTF8.GetBytes(xml);
  61.                 stream.Write(data, 0, data.Length);
  62.                 stream.Position = 0;
  63.                 DataContractSerializer deserializer = new DataContractSerializer(toType);
  64.                 return deserializer.ReadObject(stream);
  65.             }
  66.         }
  67.  
  68.         /// <summary>
  69.         /// Klonuje głęboko dowolny obiekt
  70.         /// </summary>
  71.         /// <typeparam name="T"></typeparam>
  72.         /// <param name="obj"></param>
  73.         /// <returns></returns>
  74.         public static T CloneAsXml<T>(this T obj) => obj.DeserializeFromXmlString(obj.SerializeToXmlString());
  75.  
  76.         public static string SerializeJson<T>(this T obj) => JsonConvert.SerializeObject(obj);
  77.  
  78.         public static T DeserializeJson<T>(this string json) => JsonConvert.DeserializeObject<T>(json);
  79.  
  80.         public static T CloneAsJson<T>(this T obj) => obj.SerializeJson().DeserializeJson<T>();
  81.     }
  82.  
  83.  
  84.    internal class School : Immutable<School>
  85.     {
  86.  
  87.         private class StudentReadOnly : Student
  88.         {
  89.             private readonly Student _student;
  90.  
  91.  
  92.             public string Name => _student.Name;
  93.  
  94.             public string LastName => _student.LastName;
  95.  
  96.  
  97.             internal StudentReadOnly(Student student)
  98.             {
  99.                 _student = student;
  100.             }
  101.         }
  102.  
  103.  
  104.         private readonly StudentsCollection _studentsCollection;
  105.         private readonly IEnumerable<Student> _students;
  106.  
  107.         // 1st way to prevent from accessing objects on collection and change its state
  108.         internal IReadOnlyCollection<Student> StudentsClones => _students.Select(s => s.CloneAsJson()).ToList().AsReadOnly();
  109.  
  110.         // 2st way to prevent from accessing objects on collection and change its state
  111.         internal IReadOnlyCollection<Student> StudentsReadOnly => _students.Select(s => new StudentReadOnly(s)).ToList().AsReadOnly();
  112.  
  113.  
  114.         // Cannot prevent from access public properties on collection and change its value
  115.         internal StudentsCollection StudentsCollection => _studentsCollection;
  116.  
  117.         internal School(StudentsCollection studentsCollection, IEnumerable<Student> students)
  118.         {
  119.             _studentsCollection = studentsCollection;
  120.             _students = students;
  121.         }
  122.     }
  123.  
  124.     internal class Student
  125.     {
  126.         public string Name { get; set; }
  127.         public string LastName { get; set; }
  128.     }
  129.  
  130.  
  131.     /// <summary>
  132.     /// This class can also implement ICollection<Student> => methods required for this interface such as: Add, Clear, Contains, CopyTo, Remove, IsReadOnly
  133.     /// </summary>
  134.     internal class StudentsCollection : IReadOnlyCollection<Student>
  135.     {
  136.         private IEnumerable<Student> students;
  137.  
  138.         internal StudentsCollection(IEnumerable<Student> students)
  139.         {
  140.             this.students = students.Guard(nameof(students));
  141.         }
  142.  
  143.         public IEnumerator<Student> GetEnumerator() => students.GetEnumerator();
  144.  
  145.         IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
  146.  
  147.         public void Add(Student item) => students.Append(item);
  148.  
  149.         public void Clear() => students = Enumerable.Empty<Student>();
  150.  
  151.         public bool Contains(Student item) => students.Contains(item);
  152.  
  153.         public void CopyTo(Student[] array, int arrayIndex)
  154.             => students.ToList().ForEach(s => array[arrayIndex++] = s);
  155.  
  156.         public bool Remove(Student item)
  157.         {
  158.             if (item == null)
  159.                 return false;
  160.  
  161.             students = students.Where(s => !s.Equals(item));
  162.  
  163.             return true;
  164.         }
  165.  
  166.         public int Count => students.Count();
  167.  
  168.         public bool IsReadOnly => true;
  169.     }
Advertisement
Add Comment
Please, Sign In to add comment