![.Net配置文件——反射+配置文件存储类型实例 .Net配置文件——反射+配置文件存储类型实例](https://image.shishitao.com:8440/aHR0cHM6Ly9ia3FzaW1nLmlrYWZhbi5jb20vdXBsb2FkL2NoYXRncHQtcy5wbmc%2FIQ%3D%3D.png?!?w=700&webp=1)
配置文件+反射确实去除了选择语句的繁琐,带来了优美的赶脚!
首先改进了一下类(接上文):
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
|
namespace ClassLib { /// <summary>
/// Interface IGreetingStrategy
/// </summary>
/// <remarks>Editor:v-liuhch CreateTime:2015/6/28 11:01:58</remarks>
public interface IGreetingStrategy
{
string GreetingType { get; }
void SetGreetingWords(ITextControl textContrl);
}
/// <summary>
/// Class EnglishGreeting
/// </summary>
/// <remarks>Editor:v-liuhch CreateTime:2015/6/28 11:02:38</remarks>
public class EnglishGreeting : IGreetingStrategy
{
public string GreetingType
{
get { return English; }
}
public void SetGreetingWords(ITextControl textContrl)
{
textContrl.Text = hello,readers;
}
}
/// <summary>
/// Class ChineseGreeting
/// </summary>
/// <remarks>Editor:v-liuhch CreateTime:2015/6/28 11:02:56</remarks>
public class ChineseGreeting : IGreetingStrategy
{
private string greetingType;
public ChineseGreeting(string greetingType)
{
this .greetingType = greetingType;
}
public ChineseGreeting() : this (中文) { }
public ChineseGreeting(XmlNode section) {
XmlAttribute attr = section.SelectSingleNode(params).Attributes[greetingType]; //获取属性值
greetingType = attr.Value; //为字段赋值
}
public string GreetingType
{
get { return greetingType; }
}
public void SetGreetingWords(ITextControl textContrl)
{
textContrl.Text = 你好啊,小读者!;
}
}
/// <summary>
/// Class GeneralClass:这个类可能还有很多的字段,属性,方法,这里只是简写下
/// PS:GeneralClass是一个普通的类型,这个类内部维护着IGreetingStrategy,调用的时候还是根据多态具体调用。
/// </summary>
/// <remarks>Editor:v-liuhch CreateTime:2015/6/28 11:08:04</remarks>
public class GeneralClass
{
private IGreetingStrategy gs;
public GeneralClass(IGreetingStrategy gs)
{
this .gs = gs;
}
public string GeneralProperty
{
get
{
//做一些额外的工作,这里省略
return <span sytle= "color:red" > + gs.GreetingType + </span>;
}
}
public void GeneralMethod(ITextControl textContrl)
{
//做一些额外的工作,这里省略
gs.SetGreetingWords(textContrl);
textContrl.Text = <span sytle= "color:red" > + textContrl.Text + </span>;
//省略。。。。。。。
}
}
} |
然后在配置文件中定义好我们要使用的具体类和自定义标签的处理程序:
1
2
3
4
5
6
|
<!--greetingStrategy节点及其处理程序配置--> <configsections><section name= "greetingStrategy" type= "ClassLib.GreetingConfigurationHandler,ClassLib/" >
<greetingstrategy type= "ClassLib.ChineseGreeting,ClassLib" >
<params greetingtype= "***中文问候***" > <!--构造函数的参数-->
</params></greetingstrategy></section></configsections> |
这里,ChineseGreeting是我们要使用的类,上面定义的是处理greetingStrategy的类;
接着,写这个类的具体实现:
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
|
namespace ClassLib { public class GreetingConfigurationHandler : IConfigurationSectionHandler
{
/*
处理有参数的构造函数的对象的创建:
*/
/// <summary>
/// 创建配置节处理程序。
/// </summary>
///<param name="parent">父对象。
///<param name="configContext">配置上下文对象。
///<param name="section">节 XML 节点。
/// <returns>创建的节处理程序对象。</returns>
/// <exception cref="System.NotImplementedException"></exception>
/// <remarks>Editor:v-liuhch CreateTime:2015/6/30 20:34:54</remarks>
public object Create(object parent, object configContext, System.Xml.XmlNode section)
{
//获取节点type属性的值
Type t = Type.GetType(section.Attributes[type].Value);
object obj= null ;
try
{
/* 2,在要实例的类中加入一个构造函数,接收一个XmlNode节点,将greeting_stragetgy的节点在此传递,然后在这个构造函数中进行处理;*/
//如果t包含有参数为xmlnode的构造函数,直接使用这个构造函数
Type[] paras = { typeof(XmlNode) };
ConstructorInfo constructors = t.GetConstructor(paras);
if (constructors != null )
{
object[] paramters = { section };
return Activator.CreateInstance(t, paramters); //传入读取到的构造函数的参数
}
if (section.SelectSingleNode(params) == null ) //无参数构造函数
{
obj = Activator.CreateInstance(t);
}
else //有参数构造函数
{
/*1,在此类中对策略类进行处理,取得params节点的属性值,然后传递给具体实例化的类;*/
//获取params节点的属性greetingType的值
XmlAttribute attr = section.SelectSingleNode(params).Attributes[greetingType];
object[] parameters = { attr.Value };
obj = Activator.CreateInstance(t, parameters); //传入读取到的构造函数的参数
}
}
catch (Exception)
{
return null ;
}
return obj ;
}
}
} |
在创建方法中,我们先判断ChineseGreeting类有没有一个参数为节点的构造方法,如果有的话,就直接将section当作参数,在利用反射创建类型实例的时候传进去;
如果没有这样的构造方法,我们就在这个处理类里面读取XML文件中的参数,然后在类型实例化的时候传进去;
两种方式比较,其实都是一样的,只过是这个参数读取的早晚的问题;个人对比了下,觉得在这个类里面读取配置文件中的构造函数参数的方式更加灵活,个人偏爱。
写个东西测试下:
1
2
3
4
5
6
7
8
9
|
#region 自定义节点存储类型信息——反射方法 IGreetingStrategy greetingStrategy = (IGreetingStrategy)ConfigurationManager.GetSection(greetingStrategy);
if (greetingStrategy != null )
{
GeneralClass generalClass = new GeneralClass(greetingStrategy);
ltrGreetingType.Text = generalClass.GeneralProperty;
generalClass.GeneralMethod(ltrGreetingWord);
}
#endregion
|
嘿嘿,相对方便。
感觉反射强大在把变化抽出来,但是抽出来的这个变化放到哪里去最容易改动或者是后期维护成本较低,于是配置文件这时候就该上了。。。。。。
http://www.2cto.com/kf/201507/412924.html