Skip to content

Instantly share code, notes, and snippets.

@Semior001
Created March 29, 2019 15:14
Show Gist options
  • Save Semior001/b18f94540ed6b1e6cfdaaa36e923a47b to your computer and use it in GitHub Desktop.
Save Semior001/b18f94540ed6b1e6cfdaaa36e923a47b to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Xml.Serialization;
namespace Quiz29March
{
public class Person
{
private string _name;
private List<string> _messages = new List<string>();
public Person(){}
public Person(string name)
{
_name = name;
}
public string Name
{
get => _name;
set => _name = value;
}
public List<string> Messages
{
get => _messages;
set => _messages = value;
}
public void SendMessage(Person person, string message)
{
Messages.Add("Me: " + message);
person.Messages.Add(_name + ": " + message);
}
public void SerializeToXmlFile(string fileName)
{
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (Stream stream = new FileStream(fileName, FileMode.Create))
{
serializer.Serialize(stream, this);
}
}
public static Person DeserializeFromXmlFile(string fileName)
{
XmlSerializer serializer = new XmlSerializer(typeof(Person));
using (Stream stream = new FileStream(fileName, FileMode.Open))
{
return (Person) serializer.Deserialize(stream);
}
}
public override string ToString()
{
string result = "Person:{\n" +
" Name: " + _name + "\n" +
" Messages: [\n";
foreach (string message in Messages)
{
result += " \"" + message + "\"\n";
}
result += " ]\n";
result += "}";
return result;
}
}
class Task1
{
static void Main(string[] args)
{
Person person = new Person("Person1");
Person person1 = new Person("Person2");
Person person2 = new Person("Person3");
person.SendMessage(person1, "Hello!");
person1.SendMessage(person, "Hi!");
person2.SendMessage(person, "Hi guys!");
person2.SendMessage(person1, "Hi guys!");
person.SendMessage(person1, "How are you, Person1?");
person1.SendMessage(person, "I'm a little bit crazy, cause " +
"I'm talking with an instance of the class with 3 methods and 2 fields");
person2.SendMessage(person, "Yeah, it's really strange");
person2.SendMessage(person1, "Yeah, it's really strange");
person.SendMessage(person1, "Goodbye!");
person.SerializeToXmlFile("person.xml");
person1.SerializeToXmlFile("person1.xml");
person2.SerializeToXmlFile("person2.xml");
Console.WriteLine(Person.DeserializeFromXmlFile("person.xml"));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment