0%

特性使用示例-1

实现ini文件的序列化与反序列化

反序列化

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
public class IniAttribute : Attribute
{
public string KeyName;

public IniAttribute(string keyName)
{
KeyName = keyName;
}
}

public class Result
{
[Ini("长(mm)")]
public object length;

[Ini("宽(mm)")]
public object width;

[Ini("高(mm)")]
public object height;
}

IniFile iniFile = new IniFile("D:\\Test.ini");
//读取ini文件键值对
Dictionary<string, string> resultDic = iniFile.ReadKeyValues("Test");
Result result = new Result();
//获取所有字段
foreach (var field in result.GetType().GetFields())
{
//获取字段的特性
var att = field.GetCustomAttributes(typeof(IniAttribute), true)[0] as IniAttribute;
if(att != null)
{
//特性KeyName与ini中key匹配则给对应字段赋值
if(resultDic.ContainsKey(att.KeyName))
{
Type type = field.FieldType;
if(type == typeof(string))
field.SetValue(result, resultDic[att.KeyName]);
else
field.SetValue(result, Convert.ChangeType(resultDic[att.KeyName], type));
}
}

}
//获取所有属性
foreach (var prop in result.GetType().GetProperties())
{
var att = prop.GetCustomAttributes(typeof(IniAttribute), true)[0] as IniAttribute;
if(att != null)
{
if (resultDic.ContainsKey(att.KeyName))
prop.SetValue(result, resultDic[att.KeyName], null);
}
}

进阶

解析多个Session

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
IniFile iniFile = new IniFile("D:\\Test.ini");
Dictionary<string, Dictionary<string, string>> iniResultDic = new Dictionary<string, Dictionary<string, string>>();
TestResult testResult = new TestResult();

foreach (var fieldClass in testResult.GetType().GetFields())
{

//获取特性作为Session
var attClass = fieldClass.GetCustomAttributes(typeof(IniSessionAttribute), true)[0] as IniSessionAttribute;
if(attClass != null)
{
//存储该Session下的Key Value
Dictionary<string, string> resultDic = iniFile.ReadKeyValues(attClass.SessionName);
var obj = fieldClass.GetValue(testResult);
foreach (var field in fieldClass.FieldType.GetFields())
{
//获取包含IniKey特性的字段
var att = field.GetCustomAttributes(typeof(IniKeyAttribute), true)[0] as IniKeyAttribute;
if (att != null)
{
if (resultDic.ContainsKey(att.KeyName))
{
Type type = field.FieldType;
if (type == typeof(string))
field.SetValue(obj, resultDic[att.KeyName]);
else
field.SetValue(obj, Convert.ChangeType(resultDic[att.KeyName], type));
}
}
}
iniResultDic.Add(attClass.SessionName, resultDic);
}
}

public class TestResult
{
[IniSession("Result")]
public Result result = new Result();
[IniSession("Info")]
public Info info = new Info();
}
public class Result
{
[IniKey("长(mm)")]
public double length;

[IniKey("宽(mm)")]
public object width;

[IniKey("高(mm)")]
public string height;
}
public class Info
{
[IniKey("ID")]
public int ID;

[IniKey("结果")]
public string result;
}

public class IniKeyAttribute : Attribute
{
public string KeyName;

public IniKeyAttribute(string keyName)
{
KeyName = keyName;
}
}
public class IniSessionAttribute : Attribute
{
public string SessionName;

public IniSessionAttribute(string sessionName)
{
SessionName = sessionName;
}
}

封装

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
public T Deserialize<T>(string path)
{
IniFile iniFile = new IniFile(path);
T testResult = (T)Activator.CreateInstance(typeof(T));
foreach (var fieldClass in testResult.GetType().GetFields())
{

//获取特性作为Session
var attClass = fieldClass.GetCustomAttributes(typeof(IniSessionAttribute), true)[0] as IniSessionAttribute;
if (attClass != null)
{
//存储该Session下的Key Value
Dictionary<string, string> resultDic = iniFile.ReadKeyValues(attClass.SessionName);
var obj = fieldClass.GetValue(testResult);
foreach (var field in fieldClass.FieldType.GetFields())
{
//获取包含IniKey特性的字段
var att = field.GetCustomAttributes(typeof(IniKeyAttribute), true)[0] as IniKeyAttribute;
if (att != null)
{
if (resultDic.ContainsKey(att.KeyName))
{
Type type = field.FieldType;
if (type == typeof(string))
field.SetValue(obj, resultDic[att.KeyName]);
else
field.SetValue(obj, Convert.ChangeType(resultDic[att.KeyName], type));
}
}
}
}
}
return testResult;
}

序列化

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
IniFile iniFile = new IniFile("D:\\Test1.ini");
Result result = new Result();
result.width = 235;
StringBuilder sb = new StringBuilder();
sb.AppendLine("[SessionName]")

foreach (var field in result.GetType().GetFields())
{
var att = field.GetCustomAttributes(typeof(IniAttribute), true)[0] as IniAttribute;
if (att != null)
{
object o = field.GetValue(result);
if (o != null) {
sb.AppendLine(string.Format("{0}={1}", att.KeyName, o.ToString())); }
else
sb.AppendLine(string.Format("{0}={1}", att.KeyName, ""));
}
}
File.WriteAllText("D:\\Test1.ini", sb.ToString());

进阶

序列化多个Session

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
TestResult testResult = new TestResult();
testResult.info.ID = 235;
testResult.info.result = "不合格";
testResult.result.length = 0;
testResult.result.width = 1;
testResult.result.height = "";
StringBuilder sb = new StringBuilder();

foreach (var fieldClass in testResult.GetType().GetFields())
{
//获取特性作为Session
var attClass = fieldClass.GetCustomAttributes(typeof(IniSessionAttribute), true)[0] as IniSessionAttribute;
if (fieldClass != null)
{
sb.AppendLine(string.Format("[{0}]", attClass.SessionName));
var obj = fieldClass.GetValue(testResult);
foreach (var field in fieldClass.FieldType.GetFields())
{
//获取包含IniKey特性的字段
var att = field.GetCustomAttributes(typeof(IniKeyAttribute), true)[0] as IniKeyAttribute;
if (att != null)
sb.AppendLine(string.Format("{0}={1}", att.KeyName, field.GetValue(obj)));
else
sb.AppendLine(string.Format("{0}={1}", att.KeyName, ""));
}
}
}
File.WriteAllText("D:\\Test1.ini", sb.ToString());

封装

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public string Serialize<T>(T testResult)
{
StringBuilder sb = new StringBuilder();
foreach (var fieldClass in testResult.GetType().GetFields())
{
//获取特性作为Session
var attClass = fieldClass.GetCustomAttributes(typeof(IniSessionAttribute), true)[0] as IniSessionAttribute;
if (fieldClass != null)
{
sb.AppendLine(string.Format("[{0}]", attClass.SessionName));
var obj = fieldClass.GetValue(testResult);
foreach (var field in fieldClass.FieldType.GetFields())
{
//获取包含IniKey特性的字段
var att = field.GetCustomAttributes(typeof(IniKeyAttribute), true)[0] as IniKeyAttribute;
if (att != null)
sb.AppendLine(string.Format("{0}={1}", att.KeyName, field.GetValue(obj)));
else
sb.AppendLine(string.Format("{0}={1}", att.KeyName, ""));
}
}
}
return sb.ToString();
}