C#反射应用实例
本文实例讲述了C#反射应用。分享给大家供大家参考。具体如下:
通过反射实现多系统数据库的配置
通过定义接口,反射实例化配置的节点的值
配置App.config:
<?xmlversion="1.0"encoding="utf-8"?> <configuration> <appSettings> <addkey="DAL"value="FactoryInterface.Oracle"/> </appSettings> </configuration>
通过System.Configuration.ConfigurationManager.AppSettings读取该key的value,使用Configuration需要将其dll添加到项目中
接口定义:
namespaceFactoryInterface
{
interfaceIDAL
{
voidinsert();
}
}
Program定义:
namespaceFactoryInterface
{
classProgram
{
staticvoidMain(string[]args)
{
stringconfig=System.Configuration.ConfigurationManager.AppSettings["DAL"]; Console.WriteLine(config); Typet=Type.GetType(config); IDALdal=(IDAL)System.Activator.CreateInstance(t); dal.insert(); Console.ReadKey();
}
} classMySql:IDAL{ publicvoidinsert(){ Console.WriteLine("thisdatainsertbyMySql"); } } classOracle:IDAL { publicvoidinsert() { Console.WriteLine("thisdatainsertbyOracle"); } } }
热门推荐
10 儿子立冬祝福语简短独特
11 对当兵的祝福语简短
12 侄儿高考试祝福语简短
13 伴郎红包祝福语朋友简短
14 媳妇生日简短祝福语朋友
15 公司年会祝福语简短最好
16 元旦感恩祝福语简短大全
17 红包祝福语简短10字
18 周六早晨祝福语简短