public static Type SerializationXML <Type>(Type clas, string filename, SerOpType OpType) { if (OpType == SerOpType.Deserialize && !File.Exists(filename)) { Console.WriteLine("Файла не существует"); return(default(Type)); } XmlSerializer serializer = new XmlSerializer(typeof(Type)); if (OpType == SerOpType.Serialize) { using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate)) { serializer.Serialize(fs, clas); } } if (OpType == SerOpType.Deserialize) { using (FileStream fs = new FileStream(filename, FileMode.Open)) { return((Type)serializer.Deserialize(fs)); } } return(default(Type)); }
public static Type SerializationJSON <Type>(Type clas, string filename, SerOpType OpType) { if (OpType == SerOpType.Deserialize && !File.Exists(filename)) { Console.WriteLine("Файла не существует"); return(default(Type)); } DataContractJsonSerializer formatter = new DataContractJsonSerializer(typeof(Type)); if (OpType == SerOpType.Serialize) { using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate)) { formatter.WriteObject(fs, clas); } } if (OpType == SerOpType.Deserialize) { using (FileStream fs = new FileStream(filename, FileMode.Open)) { return((Type)formatter.ReadObject(fs)); } } return(default(Type)); }