C#反射应用实例
作者:shichen2014 时间:2023-11-03 14:47:46
本文实例讲述了C#反射应用。分享给大家供大家参考。具体如下:
通过反射实现多系统数据库的配置
通过定义接口,反射实例化配置的节点的值
配置App.config:
<?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添加到项目中
接口定义:
namespace FactoryInterface
{
interface IDAL
{
void insert();
}
}
Program定义:
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");
}
}
}
输出效果如下图所示:
希望本文所述对大家的C#程序设计有所帮助。
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解JS与APP原生控件交互
![](https://img.aspxhome.com/file/2023/3/108303_0s.png)
eclipse maven 插件的安装和配置详解
![](https://img.aspxhome.com/file/2023/8/58298_0s.jpg)
Java多线程编程之读写锁ReadWriteLock用法实例
![](https://img.aspxhome.com/file/2023/0/65620_0s.png)
深入了解Java虚拟机栈以及内存模型
![](https://img.aspxhome.com/file/2023/0/112420_0s.jpg)
Mybatis 缓存原理及失效情况解析
![](https://img.aspxhome.com/file/2023/6/82996_0s.jpg)
C#中使用CAS实现无锁算法的示例详解
Eclipse下编写java程序突然不会自动生成R.java文件和包的解决办法
浅谈AnDroidDraw+DroidDraw实现Android程序UI设计的分析说明
![](https://img.aspxhome.com/file/2023/1/85991_0s.jpg)
Android开发实现SubMenu选项菜单和子菜单示例
![](https://img.aspxhome.com/file/2023/0/114280_0s.gif)
Android编程中关于单线程模型的理解与分析
SpringCloud Gateway 路由配置定位原理分析
![](https://img.aspxhome.com/file/2023/0/91120_0s.png)
idea中如何使用git进行版本回退详解
![](https://img.aspxhome.com/file/2023/2/101762_0s.jpg)
c#读取XML多级子节点
Android自定义轮播图效果
Android中解决页签手指按下从左到右滑动的bug
java 出现Zipexception 异常的解决办法
![](https://img.aspxhome.com/file/2023/7/85517_0s.png)
浅析C#静态类,静态构造函数,静态变量
Java使用雪花id生成算法详解
java实现Dijkstra最短路径算法
![](https://img.aspxhome.com/file/2023/5/71915_0s.jpg)
详解Java的Hibernate框架中的Interceptor和Collection
![](https://img.aspxhome.com/file/2023/2/72572_0s.png)