亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

C#反射應(yīng)用實(shí)例

 更新時(shí)間:2014年12月04日 09:43:08   投稿:shichen2014  
這篇文章主要介紹了C#反射應(yīng)用,實(shí)例分析了通過反射實(shí)現(xiàn)多系統(tǒng)數(shù)據(jù)庫的配置方法,是比較實(shí)用的技巧,需要的朋友可以參考下

本文實(shí)例講述了C#反射應(yīng)用。分享給大家供大家參考。具體如下:

通過反射實(shí)現(xiàn)多系統(tǒng)數(shù)據(jù)庫的配置

通過定義接口,反射實(shí)例化配置的節(jié)點(diǎn)的值

配置App.config:

復(fù)制代碼 代碼如下:
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <appSettings>
    <add key="DAL" value="FactoryInterface.Oracle"/>
  </appSettings>
</configuration>

通過System.Configuration.ConfigurationManager.AppSettings讀取該key的value,使用Configuration需要將其dll添加到項(xiàng)目中

接口定義:

復(fù)制代碼 代碼如下:
namespace FactoryInterface
{
    interface IDAL
    {
        void insert();
    }
}

Program定義:

復(fù)制代碼 代碼如下:
namespace FactoryInterface
{
    class Program
    {
        static void Main(string[] args)
        {

            string config = System.Configuration.ConfigurationManager.AppSettings["DAL"];
            Console.WriteLine(config);
            Type t = Type.GetType(config);
            IDAL dal =(IDAL) System.Activator.CreateInstance(t);
            dal.insert();
            Console.ReadKey();

        }

    }
    class MySql : IDAL {
        public void insert() {
            Console.WriteLine("this data insert by MySql");
        }
    }
    class Oracle : IDAL
    {
        public void insert()
        {
            Console.WriteLine("this data insert by Oracle");
        }
    }
}

輸出效果如下圖所示:

希望本文所述對(duì)大家的C#程序設(shè)計(jì)有所幫助。

相關(guān)文章

最新評(píng)論