d23004
2023-05-24 fa87a558389d9d425f70afa1c8de54c69875ef2c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
using System;
using System.IO;
using System.Reflection;
using System.Xml.Serialization;
 
 
namespace SA_LTT.Base
{
    public abstract class XmlManager<T> 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;
        }
    }
}