Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class SerializeExtensions
- {
- /// <summary>
- /// Odtwarza dowolny obiekt ze serializowanego stringa xml
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static string SerializeToXmlString<T>(this T obj)
- {
- XmlSerializer xmlSerializer = new XmlSerializer(obj.GetType());
- using (StringWriter textWriter = new StringWriter())
- {
- xmlSerializer.Serialize(textWriter, obj);
- return textWriter.ToString();
- }
- }
- /// <summary>
- /// Dserializuje dowolny obiekt ze stringa xml
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <param name="s"></param>
- /// <returns></returns>
- public static T DeserializeFromXmlString<T>(this T obj, string s)
- {
- var serializer = new XmlSerializer(obj.GetType());
- T result;
- using (TextReader reader = new StringReader(s))
- {
- result = (T)serializer.Deserialize(reader);
- }
- obj = result;
- return result;
- }
- public static string SerializeToXmlStringDc(this object obj)
- {
- using (MemoryStream memoryStream = new MemoryStream())
- using (StreamReader reader = new StreamReader(memoryStream))
- {
- DataContractSerializer serializer = new DataContractSerializer(obj.GetType());
- serializer.WriteObject(memoryStream, obj);
- memoryStream.Position = 0;
- return reader.ReadToEnd();
- }
- }
- public static object DeserializeFromXmlStringDc(this string xml, Type toType)
- {
- using (Stream stream = new MemoryStream())
- {
- byte[] data = Encoding.UTF8.GetBytes(xml);
- stream.Write(data, 0, data.Length);
- stream.Position = 0;
- DataContractSerializer deserializer = new DataContractSerializer(toType);
- return deserializer.ReadObject(stream);
- }
- }
- /// <summary>
- /// Klonuje głęboko dowolny obiekt
- /// </summary>
- /// <typeparam name="T"></typeparam>
- /// <param name="obj"></param>
- /// <returns></returns>
- public static T CloneAsXml<T>(this T obj) => obj.DeserializeFromXmlString(obj.SerializeToXmlString());
- public static string SerializeJson<T>(this T obj) => JsonConvert.SerializeObject(obj);
- public static T DeserializeJson<T>(this string json) => JsonConvert.DeserializeObject<T>(json);
- public static T CloneAsJson<T>(this T obj) => obj.SerializeJson().DeserializeJson<T>();
- }
- internal class School : Immutable<School>
- {
- private class StudentReadOnly : Student
- {
- private readonly Student _student;
- public string Name => _student.Name;
- public string LastName => _student.LastName;
- internal StudentReadOnly(Student student)
- {
- _student = student;
- }
- }
- private readonly StudentsCollection _studentsCollection;
- private readonly IEnumerable<Student> _students;
- // 1st way to prevent from accessing objects on collection and change its state
- internal IReadOnlyCollection<Student> StudentsClones => _students.Select(s => s.CloneAsJson()).ToList().AsReadOnly();
- // 2st way to prevent from accessing objects on collection and change its state
- internal IReadOnlyCollection<Student> StudentsReadOnly => _students.Select(s => new StudentReadOnly(s)).ToList().AsReadOnly();
- // Cannot prevent from access public properties on collection and change its value
- internal StudentsCollection StudentsCollection => _studentsCollection;
- internal School(StudentsCollection studentsCollection, IEnumerable<Student> students)
- {
- _studentsCollection = studentsCollection;
- _students = students;
- }
- }
- internal class Student
- {
- public string Name { get; set; }
- public string LastName { get; set; }
- }
- /// <summary>
- /// This class can also implement ICollection<Student> => methods required for this interface such as: Add, Clear, Contains, CopyTo, Remove, IsReadOnly
- /// </summary>
- internal class StudentsCollection : IReadOnlyCollection<Student>
- {
- private IEnumerable<Student> students;
- internal StudentsCollection(IEnumerable<Student> students)
- {
- this.students = students.Guard(nameof(students));
- }
- public IEnumerator<Student> GetEnumerator() => students.GetEnumerator();
- IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
- public void Add(Student item) => students.Append(item);
- public void Clear() => students = Enumerable.Empty<Student>();
- public bool Contains(Student item) => students.Contains(item);
- public void CopyTo(Student[] array, int arrayIndex)
- => students.ToList().ForEach(s => array[arrayIndex++] = s);
- public bool Remove(Student item)
- {
- if (item == null)
- return false;
- students = students.Where(s => !s.Equals(item));
- return true;
- }
- public int Count => students.Count();
- public bool IsReadOnly => true;
- }
Advertisement
Add Comment
Please, Sign In to add comment