1.接口
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBase.Interface
{
// 数据库 接口
public interface IDBInterface
{
// 查询方法
void Query();
}
}
2.SQL数据库(类库 DataBase.Sql.dll)
using DataBase.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBase.Sql
{
// SQl类
public class SqlServerHelper : IDBInterface
{
public SqlServerHelper()
{
Console.WriteLine($"{this.GetType().Name} 被构造");
}
// 接口继承
public void Query()
{
Console.WriteLine($"{this.GetType().Name}.Query");
}
}
}
3.Mysql数据库(DataBase.Mysql.dll)
using DataBase.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBase.Mysql
{
// Mysql 数据库
public class MySqlHelper : IDBInterface
{
// 构造函数
public MySqlHelper()
{
Console.WriteLine($"{this.GetType().Name} 被构造");
}
// 查询
public void Query()
{
Console.WriteLine($"{this.GetType().Name}.Query");
}
}
}
4.工厂类(读取配置文件)
配置文件
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<appSettings>
<add key="IDBHelperConfig" value="DataBase.Sql.SqlServerHelper, DataBase.Sql"/>
</appSettings>
</configuration>
using DataBase.Interface;
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace ReflectionFactory
{
public class Factory
{
// 读取配置文件
public static string IDBHelperConfig = ConfigurationManager.AppSettings["IDBHelperConfig"];
// Dll 文件名
private static string DllName = IDBHelperConfig.Split(',')[1];
// 反射 类名
private static string TypeName = IDBHelperConfig.Split(',')[0];
// 工厂创建函数
public static IDBInterface CreateHelper()
{
Console.WriteLine("加载DLL:" + DllName);
// 反射加载Dll程序集
Assembly assembly = Assembly.Load(DllName);
Console.WriteLine($"获取类: {TypeName}");
// 获取类的 Type
Type typeHelper = assembly.GetType(TypeName);
// 创建对象
object oHelper = Activator.CreateInstance(typeHelper);
// 类型转换为 接口类型
IDBInterface iDbHelper = (IDBInterface)oHelper;
return iDbHelper;
}
}
}
5.调用测试
using DataBase.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace ReflectionFactory
{
class Program
{
static void Main(string[] args)
{
#region 反射与工厂模式
Console.WriteLine("-----反射与工厂模式----------");
// 使用工厂模式创建 对象(接口)
IDBInterface iDBInterface = Factory.CreateHelper();
Console.WriteLine("-----调用对象函数----------");
// 调用对象函数
iDBInterface.Query();
#endregion
Console.ReadLine();
}
}
}
结果
SQL
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<appSettings>
<add key="IDBHelperConfig" value="DataBase.Sql.SqlServerHelper, DataBase.Sql"/>
</appSettings>
</configuration>
Mysql
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2" />
</startup>
<appSettings>
<add key="IDBHelperConfig" value="DataBase.Mysql.MySqlHelper, DataBase.Mysql"/>
</appSettings>
</configuration>