using System; using System.IO; using System.Reflection; using System.Xml.Serialization; namespace SA_LTT.Base { public abstract class XmlManager where T : class { string _extension = ".xml"; public XmlManager() { } protected void SaveFile(string filePath, T data) { try { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (TextWriter textWriter = new StreamWriter(filePath)) { xmlSerializer.Serialize(textWriter, data); textWriter.Close(); } } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace); } } protected bool TrySaveFile(string filePath, T data) { try { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (TextWriter textWriter = new StreamWriter(filePath)) { xmlSerializer.Serialize(textWriter, data); textWriter.Close(); } return true; } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace); return false; } } protected T ReadFile(string filePath) { try { T data; XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (TextReader textReader = new StreamReader(filePath)) { data = (T)xmlSerializer.Deserialize(textReader); textReader.Close(); } return data; } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace); return null; } } protected bool TryReadFile(string filePath, out T data) { try { XmlSerializer xmlSerializer = new XmlSerializer(typeof(T)); using (TextReader textReader = new StreamReader(filePath)) { data = (T)xmlSerializer.Deserialize(textReader); textReader.Close(); } return true; } catch (Exception e) { EquipmentLogManager.Instance.WriteExceptionLog(e.StackTrace); data = null; return false; } } protected void DeleteFile(string filePath) { FileInfo fileInfo = new FileInfo(filePath); if(fileInfo.Exists) { fileInfo.Delete(); } } public void Copy(T value) { foreach (PropertyInfo propertyInfo in this.GetType().GetProperties()) { // Set Method 가 없으면 따로 값을 넣지 않음. if(propertyInfo.GetSetMethod() != null) { propertyInfo.SetValue(this, propertyInfo.GetValue(value, null), null); } } } public T Clone() { T Tvalue = (T)this.MemberwiseClone(); return Tvalue; } } }