> 만들 예정입니다!
먼저 클래스 개선(연결됨): 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>";
//省略。。。。。。。
}
}
}
<!--greetingStrategy节点及其处理程序配置-->
<configSections>
<section name="greetingStrategy" type="ClassLib.GreetingConfigurationHandler,ClassLib"/>
</configSections>
<greetingStrategy type="ClassLib.ChineseGreeting,ClassLib">
<params greetingType="***中文问候***"/> <!--构造函数的参数-->
</greetingStrategy>
다음으로 이 클래스의 특정 구현을 작성합니다.
namespace ClassLib
{
public class GreetingConfigurationHandler : IConfigurationSectionHandler
{
/*
处理有参数的构造函数的对象的创建:
*/
/// <summary>
/// 创建配置节处理程序。
/// </summary>
/// <param name="parent">父对象。</param>
/// <param name="configContext">配置上下文对象。</param>
/// <param name="section">节 XML 节点。</param>
/// <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 ;
}
}
}
테스트할 내용 작성:
#region 自定义节点存储类型信息——反射方法
IGreetingStrategy greetingStrategy = (IGreetingStrategy)ConfigurationManager.GetSection("greetingStrategy");
if (greetingStrategy != null)
{
GeneralClass generalClass = new GeneralClass(greetingStrategy);
ltrGreetingType.Text = generalClass.GeneralProperty;
generalClass.GeneralMethod(ltrGreetingWord);
}
#endregion
비용이 저렴하므로 구성 파일은 이때 업로드 되었습니다. . . . . .
위는 .Net 구성 파일 - 리플렉션 + 구성 파일 저장형 인스턴스의 내용입니다. 더 많은 관련 내용은 PHP 중국어 홈페이지(www.php.cn)를 참고해주세요!