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 我要结婚祝福语简短
18 婚前女儿生日祝福语简短