当前位置:网站首页>C reflection and factory mode
C reflection and factory mode
2022-07-21 05:06:00 【Tingyi -- flying bird】
1. Interface
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace DataBase.Interface
{
// database Interface
public interface IDBInterface
{
// Query methods
void Query();
}
}
2.SQL database ( Class library 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 class
public class SqlServerHelper : IDBInterface
{
public SqlServerHelper()
{
Console.WriteLine($"{
this.GetType().Name} Constructed ");
}
// Interface inheritance
public void Query()
{
Console.WriteLine($"{
this.GetType().Name}.Query");
}
}
}
3.Mysql database (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 database
public class MySqlHelper : IDBInterface
{
// Constructors
public MySqlHelper()
{
Console.WriteLine($"{
this.GetType().Name} Constructed ");
}
// Inquire about
public void Query()
{
Console.WriteLine($"{
this.GetType().Name}.Query");
}
}
}
4. Factory ( Read configuration file )
The configuration file
<?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
{
// Read configuration file
public static string IDBHelperConfig = ConfigurationManager.AppSettings["IDBHelperConfig"];
// Dll file name
private static string DllName = IDBHelperConfig.Split(',')[1];
// Reflection Class name
private static string TypeName = IDBHelperConfig.Split(',')[0];
// Factory creation function
public static IDBInterface CreateHelper()
{
Console.WriteLine(" load DLL:" + DllName);
// Reflection loading Dll Assembly
Assembly assembly = Assembly.Load(DllName);
Console.WriteLine($" Get class : {
TypeName}");
// Get the Type
Type typeHelper = assembly.GetType(TypeName);
// Create objects
object oHelper = Activator.CreateInstance(typeHelper);
// Type conversion to Interface type
IDBInterface iDbHelper = (IDBInterface)oHelper;
return iDbHelper;
}
}
}
5. Call tests
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 Reflection and factory mode
Console.WriteLine("----- Reflection and factory mode ----------");
// Created using the factory pattern object ( Interface )
IDBInterface iDBInterface = Factory.CreateHelper();
Console.WriteLine("----- Call the object function ----------");
// Call the object function
iDBInterface.Query();
#endregion
Console.ReadLine();
}
}
}
result
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>
边栏推荐
- 關於XML 編輯工具
- 每日一题:链表内指定区间反转
- Isn't it too much to play Gobang in idea?
- 【详细教程】一文参透 MongoDB 聚合查询
- Two dimensional convolution Chinese microblog emotion classification project
- IP command usage guide
- Master these plug-ins and improve your office efficiency by 90% every minute!
- Auto encoder
- [cloud native kubernetes] how to use the initialization container under the kubernetes cluster
- C语言动态内存管理
猜你喜欢
Wangeditor uncaught (in promise) error: the editor instance already exists in the initialization node, and the edit cannot be created repeatedly
Unlock high scores | eBay deepens user experience and optimizes large screen device applications
C# 反射与工厂模式
VAE图片生成实战
数论基础-
Dynamic password lock based on stm32
通过STM32 stlink utility工具对ST-LINK芯片信息进行读取和升级以及SWD烧录媒介
每日一题:链表内指定区间反转
mpf4_ Pricing European American barrier options_ CRR_ Leisen-Reimer_ Greeks_ Binary tree trigeminal tree grid_ Fine differences (explicit implicit) crank Nicolson_ Imp volatility
2022年暑假ACM热身练习3
随机推荐
HCIP-7. LSA header of OSPF
selenium框架操作stealth.min.js文件隐藏浏览器指纹特征
Hyperchain超块链史兴国接受36氪采访:客户合作咨询量都在翻倍
[FPGA tutorial case 31] communication case 1 - ask modulation signal generation based on FPGA
9 suggestions for quickly improving programming ability
[team PK competition] the points mall has been opened | exchange exclusive gifts now
QQ Three Party login - pre environment and interaction
A very simple and beautiful login box
RNA 24. Timer, an online gadget for TCGA based analysis of immune infiltrating cells in SCI articles
Deep Residual Learning for Image Recognition--ResNet经典论文
XMind中拷贝和复制的区别
What are the six access database objects? What is the most important difference between access and excel?
Keras MNIST Handwritten Digital Data Set Digital Recognition
What does polardb for Postgres SQL mainly say?
Popular explanation: the difference between IAAs, PAAS and SaaS
阶跃数特征
程序环境和预处理
VAE image generation practice
原生threeJS入门
Kotlin学习之json数据解析