日韩黑丝制服一区视频播放|日韩欧美人妻丝袜视频在线观看|九九影院一级蜜桃|亚洲中文在线导航|青草草视频在线观看|婷婷五月色伊人网站|日本一区二区在线|国产AV一二三四区毛片|正在播放久草视频|亚洲色图精品一区

分享

C# 靜態(tài)工廠利用反射機(jī)制動(dòng)態(tài)構(gòu)建配置文件中 所配置的類的對(duì)象

 NaturalWill 2014-05-19

在產(chǎn)品項(xiàng)目進(jìn)行中我們難免會(huì)遇到因?yàn)轫?xiàng)目服務(wù)對(duì)象不同,實(shí)現(xiàn)也不同的功能。

這樣每次修改后重新編譯整個(gè)解決方案后更新耗時(shí)耗力。

其實(shí)使用靜態(tài)工廠,我們可以很容易的引入額外的dll來(lái)動(dòng)態(tài)的配置這些功能的實(shí)現(xiàn)

 

首先下面是所有工廠的基類,其中包含了一個(gè)讀取配置文件的方法,和一個(gè)生成對(duì)象的靜態(tài)方法

 

復(fù)制代碼
代碼

  /// <summary>
    /// 所有工廠基類
    ///<author name="Jan.David"></author>
    ///<![CDATA[2010-11-05]]>
    /// </summary>
public abstract class AbstractFactory
{
/// <summary>
/// 讀取設(shè)置文件內(nèi)的值
/// </summary>
/// <param name="parentNode"></param>
/// <param name="key"></param>
/// <returns></returns>
public static string ReadSettring(string parentNode,string key)
{
string FilePath = AppDomain.CurrentDomain.BaseDirectory;//項(xiàng)目路徑
//新建xml文檔對(duì)象
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
//讀取文檔
doc.Load(FilePath + "/Setting.xml");
//獲取根節(jié)點(diǎn)
System.Xml.XmlNodeList Root = doc.FirstChild.NextSibling.ChildNodes;

System.Xml.XmlNodeList nodes = default(System.Xml.XmlNodeList);
foreach (System.Xml.XmlNode node in Root)
{
if (node.Name.ToLower() == parentNode.ToLower())
{
nodes = node.ChildNodes;
break;
}
}
if (nodes != null && nodes.Count > 0)
{
foreach (System.Xml.XmlNode node in nodes)
{
if (node.Attributes["name"].Value.ToLower() == key.ToLower())
{
return node.Attributes["value"].Value;
}
}
}
return null;
}
/// <summary>
/// 實(shí)例化一個(gè)對(duì)象
/// </summary>
/// <typeparam name="T">要實(shí)例化的類型</typeparam>
/// <param name="ClassName">需要實(shí)例化的類名</param>
/// <param name="obj">實(shí)例化對(duì)象存儲(chǔ)的變量</param>
public abstract void CreateInstanc<T>(string className,ref T obj);
}
復(fù)制代碼

 

下面是具體的實(shí)現(xiàn)

 

 

代碼

 

 

配置文件

 

復(fù)制代碼
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<BillCode>
<add name="Returns" value ="IBillCodeImplt.Returns"></add>
</BillCode>

</configuration>
復(fù)制代碼

 

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多