cSharp:use Activator.CreateInstance with an Interface?
///<summary>
///数据访问工厂
///生成時間2015-2-13 10:54:34
///塗聚文(Geovin Du)
/// (利用工厂模式+反射机制+缓存机制,实现动态创建不同的数据层对象接口)
///</summary>
public class AbstractFactory
{ protected static string path = ConfigurationManager.AppSettings["WebDAL"]; //"DAL";//生成的DLL文件名 /// <summary>
/// 数据库类型
/// </summary>
protected static string dalType
{ get
{
if (path.Contains("AccessDAL"))
{
//path = "AcessDAL";
return "AccessDAL";
}
else if (path=="DAL")
{
// path = "DAL";//生成的DLL文件名
return "SqlServerDAL";//命名空间名称
}
else if (path.Contains("MySQLDAL"))
{
return "MySQLDAL";
}
else if (path.Contains("DALSQLite"))
{
//path = "DALSQLite";
return "SQLiteDAL";
}
else if (path.Contains("PostgreSQLDAL"))
{
// path = "DALPostgreSQL";
return "PostgreSQLDAL";
}
else if (path.Contains("OracleDAL"))
{
return "OracleDAL";
}
else
{
//path = "DAL";
return "SqlServerDAL";
}
}
} public AbstractFactory()
{ } ///<summary>
///分页接口
/// </summary>
///<returns></returns>
public static ISelectPage CreateSelectPage()
{
//ISelectPage page = new SelectPage();
//return page;
string className = "GeovinDu." + dalType + ".SelectPage";
return (ISelectPage)Assembly.Load(path).CreateInstance(className); //命名空间名称GeovinDu.SqlServerDAL
//path = "AcessDAL";//生成的DLL文件名 } ///<summary>
///CompanyBranch接口
/// </summary>
///<returns></returns>
public static ICompanyBranch CreateCompanyBranch(string _databaseprefix, string _sysdatarolefix)
{
//ICompanyBranch iCompanyBranch = new CompanyBranchDAL();
//return iCompanyBranch;
string className = "GeovinDu." + dalType + ".CompanyBranchDAL";//(_databaseprefix, _sysdatarolefix)
return (ICompanyBranch)Assembly.Load(path).CreateInstance(className); //GeovinDu.SqlServerDAL
} ///<summary>
///EnterpriseType接口
/// </summary>
///<returns></returns>
public static IEnterpriseType CreateEnterpriseType(string _databaseprefix, string _sysdatarolefix)
{
//IEnterpriseType iEnterpriseType = new EnterpriseTypeDAL();
//return iEnterpriseType;
string className = "GeovinDu." + dalType + ".EnterpriseTypeDAL";
return (IEnterpriseType)Assembly.Load(path).CreateInstance(className);
} ///<summary>
///LoginDiaryList接口
/// </summary>
///<returns></returns>
public static ILoginDiaryList CreateLoginDiaryList(string _databaseprefix, string _sysdatarolefix)
{
//ILoginDiaryList iLoginDiaryList = new LoginDiaryListDAL();
//return iLoginDiaryList;
string className = "GeovinDu." + dalType + ".LoginDiaryListDAL";//(_databaseprefix, _sysdatarolefix)
return (ILoginDiaryList)Assembly.Load(path).CreateInstance(className);
} ///<summary>
///OperatingUser接口
///https://msdn.microsoft.com/zh-cn/library/ms173128(VS.80).aspx
///https://stackoverflow.com/questions/2202381/reflection-how-to-invoke-method-with-parameters
/// </summary>
///<returns></returns>
public static IOperatingUser CreateOperatingUser(string _databaseprefix, string _sysdatarolefix)
{
//IOperatingUser iOperatingUser = new OperatingUserDAL(_databaseprefix, _sysdatarolefix);
//return iOperatingUser;
IOperatingUser iOperatingUser = null;
string className = "GeovinDu." + dalType + ".OperatingUserDAL";//(_databaseprefix, _sysdatarolefix)
//return (IOperatingUser)Assembly.Load(path).CreateInstance(className); //string className = "GeovinDu.SQLiteDAL.OperatingUserDAL";
//Assembly assembly = Assembly.LoadFrom(@"G:\winxp备份\PayrollPrint4\PayrollPrint\bin\Release\DALSQLite.dll");
// object oo = assembly.CreateInstance(className); //得到对象命名空间名
// MethodInfo methodinfo = assembly.GetType("GeovinDu.GeovinDu.SQLiteDAL.OperatingUserDAL").GetMethod("OperatingUserDAL"); //得到方法
//Object obj = methodinfo.Invoke(oo, new object[] { _databaseprefix, _sysdatarolefix });
//IOperatingUser iOperatingUser = (IOperatingUser)obj;
object obj = Assembly.Load(path).CreateInstance(className);
Type t2 = obj.GetType();// Type.GetType("GeovinDu.SQLiteDAL.OperatingUserDAL", false, true);
if (t2 != null)
{
iOperatingUser = (OperatingUserDAL)Activator.CreateInstance(t2, new object[] { }); //Assembly.Load(path).CreateInstance(className); //值為空
} //iOperatingUser = (IOperatingUser)Activator.CreateInstance(System.Type.GetType(className)); return iOperatingUser; } ///<summary>
///PrintWordDocumentTemplateList接口
/// </summary>
///<returns></returns>
public static IPrintWordDocumentTemplateList CreatePrintWordDocumentTemplateList(string _databaseprefix, string _sysdatarolefix)
{
//IPrintWordDocumentTemplateList iPrintWordDocumentTemplateList = new PrintWordDocumentTemplateListDAL();
//return iPrintWordDocumentTemplateList;
string className = "GeovinDu." + dalType + ".PrintWordDocumentTemplateListDAL";//(_databaseprefix, _sysdatarolefix)
return (IPrintWordDocumentTemplateList)Assembly.Load(path).CreateInstance(className);
} /// <summary>
///
/// </summary>
/// <returns></returns>
public static ISysConfig CreateSysConfig()
{ string className = "GeovinDu." + dalType + ".SysConfigDAL";
return (ISysConfig)Assembly.Load(path).CreateInstance(className);
//databaseprefix = _databaseprefix;
//ISysConfig iSysConfig = new DAL.SysConfigDAL();
//return iSysConfig;
}
}
https://github.com/fabriciorissetto/CustomActivator
https://www.codeproject.com/Articles/55710/Reflection-in-NET
https://www.codeproject.com/Articles/14593/A-General-Fast-Method-Invoker
https://github.com/ekonbenefits/impromptu-interface
https://www.codeproject.com/Articles/109868/General-DynamicObject-Proxy-and-Fast-Reflection-Pr
https://www.codeproject.com/articles/15089/an-abstract-factory-using-app-config-and-reflectio
https://www.microsoft.com/net/download/windows
/// <summary>
/// 无构造函数的用法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dllname"></param>
/// <param name="fullClassName"></param>
/// <returns></returns>
public static T CreateInstance<T>(string dllname, string fullClassName)
{
//string fullClassName = nameSpace + "." + className;
return (T)Assembly.Load(dllname).CreateInstance(fullClassName);
}
/// <summary>
/// 有構造函數的用法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dllname"></param>
/// <param name="fullName"></param>
/// <param name="parameters">构造函数的参数</param>
/// <returns></returns>
public static T CreateInstance<T>(string dllname, string fullName, object[] parameters)
{
try
{
//string fullName = nameSpace + "." + className;//命名空间.类型名
object ect = Assembly.Load(dllname).CreateInstance(fullName, true, System.Reflection.BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, parameters, null, null);//加载程序集,创建程序集里面的 命名空间.类型名 实例
return (T)ect;//类型转换并返回
}
catch
{
//发生异常,返回类型的默认值
return default(T);
}
}
/// <summary>
/// 有構造函數的用法
/// geovindu
/// 塗聚文 涂聚文
/// Geovin Du
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fullTypeName"></param>
/// <param name="paramArray">构造函数的参数</param>
/// <returns></returns>
public static T CreateInstance<T>(string fullTypeName, object[] parameters)
{
return (T)Activator.CreateInstance(Type.GetType(fullTypeName), parameters);
}
/// <summary>
/// 有構造函數的用法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="dllname"></param>
/// <param name="fullTypeName"></param>
/// <param name="paramArray"></param>
/// <returns></returns>
public static T CreateInstanceActivator<T>(string dllname, string fullTypeName, object[] paramArray)
{
Assembly tempAssembly = Assembly.Load(dllname);
Type typeofControl = tempAssembly.GetType(fullTypeName);
return (T)Activator.CreateInstance(typeofControl, paramArray);
}
/// <summary>
/// 有構造函數的用法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fullTypeName"></param>
/// <param name="paramArray"></param>
/// <returns></returns>
//public static T CreateInstance<T>(string fullTypeName, params object[] paramArray)
//{
// return (T)Activator.CreateInstance(Type.GetType(fullTypeName), args: paramArray);
//}
/// <summary>
/// 无构造函数的用法
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="fullTypeName"></param>
/// <returns></returns>
public static T CreateInstance<T>(string fullTypeName)
{
return (T)Activator.CreateInstance(Type.GetType(fullTypeName));
}
string dir = Path.GetDirectoryName(Assembly.GetEntryAssembly().Location);
string pdll = dir + @"\DALSQLite.dll";
Type type = Type.GetType("System.String");
object obj = Assembly.Load("DALSQLite").CreateInstance(className, true, System.Reflection.BindingFlags.Default | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic, null, parameter, null, null);//className
//构造函数 Type t2 = obj.GetType();
object oodu = Activator.CreateInstance(t2,parameter);
//t2.GetConstructors
if (oodu != null)
{
string du = oodu.ToString();
} string[] str = { _databaseprefix, _sysdatarolefix }; //Type type = Type.GetType("System.String");
Assembly pluginAssembly = Assembly.LoadFrom(pdll);
Type dynclasstype = pluginAssembly.GetType(className); Type[] pt = new Type[2];
pt[0] = typeof(string);// typeof(string);
pt[1] = typeof(string);//typeof(string);
//object dynobj = Activator.CreateInstance(dynclasstype);//有構造函數不可以用
object dynobj = Activator.CreateInstance(dynclasstype,parameter);
if (dynobj != null)
{
dynobj.ToString();
ConstructorInfo[] cinfos = dynclasstype.GetConstructors();// (pt);
ConstructorInfo cinfo = cinfos[0];// dynclasstype.GetConstructor(pt); if (cinfo != null)
{
string na = cinfo.Name;
ParameterInfo[] psci = cinfo.GetParameters();
foreach (ParameterInfo pici in psci) //遍历并打印所该构造函数的所有参数
{
string nci=pici.ParameterType.ToString() + " :" + pici.Name + ",";
} }
} Type classType = pluginAssembly.GetType(className);
StringBuilder g = new StringBuilder();
ConstructorInfo[] ci = t2.GetConstructors(); //获取类的所有构造函数
foreach (ConstructorInfo c in ci) //遍历每一个构造函数
{
c.ToString();
ParameterInfo[] ps = c.GetParameters(); //取出每个构造函数的所有参数
foreach (ParameterInfo pi in ps) //遍历并打印所该构造函数的所有参数
{
g.Append(pi.ParameterType.ToString() + " :" + pi.Name + ",");
} }
cSharp:use Activator.CreateInstance with an Interface?的更多相关文章
- EF Core使用SQL调用返回其他类型的查询 ASP.NET Core 2.0 使用NLog实现日志记录 CSS 3D transforms cSharp:use Activator.CreateInstance with an Interface? SqlHelper DBHelper C# Thread.Abort方法真的让线程停止了吗? 注意!你的Thread.Abort方法真
EF Core使用SQL调用返回其他类型的查询 假设你想要 SQL 本身编写,而不使用 LINQ. 需要运行 SQL 查询中返回实体对象之外的内容. 在 EF Core 中,执行该操作的另一种方法 ...
- Activator.CreateInstance 方法 (Type) 的用法
转自:http://www.cnblogs.com/lmfeng/archive/2012/01/30/2331666.html Activator.CreateInstance 方法 (Type) ...
- 用Activator.CreateInstance代替new实现类的实例化(转)
一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例. 这在工厂模式中是非常有用的 这样,可以使程序有更高的扩展性,例如,,下面的例子 如果现在有一个类,专门用来计算交通工具的速度, ...
- C# Activator.CreateInstance()
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- C# Activator.CreateInstance 动态创建类的实例(二)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...
- 用Activator.CreateInstance代替new实现类的实例化
一直想得到这样一个函数,输入一个类的名称为参数,返回一个相应的类的实例. 这在工厂模式中是非常有用的 这样,可以使程序有更高的扩展性,例如,,下面的例子 如果现在有一个类,专门用来计算交通工具的速度, ...
- C# Activator.CreateInstance()方法使用
C#在类工厂中动态创建类的实例,所使用的方法为: 1. Activator.CreateInstance (Type) 2. Activator.CreateInstance (Type, Objec ...
- 注意Activator.CreateInstance两个重载方法的性能
今天扩展一个Type的扩展方法New: public static object New(this Type type, params object[] args) { Guard.ArgumentN ...
- 关于Assembly.CreateInstance()与Activator.CreateInstance()方法
于Assembly.CreateInstance()与Activator.CreateInstance()方法 动 态创建类对象,大多是Activator.CreateInstance()和Activ ...
随机推荐
- Presto + Superset 数据仓库及BI
基于Presto和superset搭建数据分析平台. Presto可以作为数据仓库,能够连接多种数据库和NoSql,同时查询性能很高: Superset提供了Presto连接,方便数据可视化和dash ...
- 9.2 翻译系列:数据注解特性之---Column【EF 6 Code First系列】
原文链接:http://www.entityframeworktutorial.net/code-first/column-dataannotations-attribute-in-code-firs ...
- u-boot中debug的一些总结
研究u-boot,首要搞清楚的是代码的流程,运行流程是什么样子的呢?不知道,就看log.这就要把log信息 打开.研究u-boot的文件,发现里面是很多DEBUG宏定义的打印,这个打印着怎么打开呢? ...
- Go语言标准库之time
Go语言标准库之time 时间的格式化和解析 格式化 Format Go语言和其他语言的时间格式化的方式不同,Go语言格式化的方式更直观,其他的语言一般是yyyy-mm-dd package main ...
- spring boot 集成freemarker
- Python常用模块——json & pickle
序列化模块 1.什么是序列化-------将原本的字典,列表等对象转换成一个字符串的过程就叫做序列化 2.序列化的目的 1.以某种存储形式使自定义对象持久化 2.将对象从一个地方传递到另一个地方 3. ...
- Git使用教程:最详细、最傻瓜、最浅显、真正手把手教!
Git使用教程:最详细.最傻瓜.最浅显.真正手把手教! 蘇小小 Web项目聚集地 9月16日 作者 | 蘇小小 编辑 | 王久一 来源 | 慕课网 导读:因为教程详细,所以行文有些长,新手边看边操作效 ...
- es6中的对象的可计算的属性名
先简单的啰嗦一下对象的属性: var obj = { a:2 } 要访问obj中a的位置,方法:1. obj.a //2 2..obj ["a"] ...
- UIAutomatorViewer增加xpath查看
原来的UIAutomatorViewer是没有xpath查看路径的,下载如下jar包: https://pan.baidu.com/s/1jpr6m0OOce7CtnDE_lIJ9A 密码:cja0 ...
- .NetCore使用skywalking实现实时性能监控
一.简介 很久之前写了一篇 <.Net Core 2.0+ InfluxDB+Grafana+App Metrics 实现跨平台的实时性能监控>关于NetCore性能监控的文章,使用Inf ...