欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

Csharp:use Activator.CreateInstance with an Interface?

程序员文章站 2022-04-08 15:42:59
https://github.com/fabriciorissetto/CustomActivator https://www.codeproject.com/Articles/55710/Reflection-in-NET ......
	///<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://*.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