通过反射创建Sqlite的数据库操作实体

2017-05-02 14:38:06  访问(1447) 赞(0) 踩(0)


        /// <summary>
        /// 获得DLL的根目录
        /// </summary>
        /// <returns></returns>
        private static string GetRootPath()
        {
            Assembly ass = typeof(DBDriver).Assembly;

            FileInfo theInfo = new FileInfo(ass.Location);

            string theResult = theInfo.Directory.FullName;

            theInfo = null;

            return theResult;
        }


        /// <summary>
        /// 通过反射创建类对象
        /// </summary>
        /// <returns></returns>
        private static IDataBaseHelper SQLiteHelperCreate()
        {
           
            const string DLLConfigName
                = 
                "SlowX.DAL.DLLDriver.DBDriver.SQLiteHelper.DLL";

            const string TypeConfigName
                =
                "SlowX.DAL.DLLDriver.DBDriver.SQLiteHelper.TypeName";

            // 比如
            // ~/SlowX.SQLiteX86Helper.dll
            string strDLL 
                = 
                ConfigurationManager.AppSettings[DLLConfigName];

            // SlowX.SQLiteX86Helper.DBHelper
            string TypeName 
                = 
                ConfigurationManager.AppSettings[TypeConfigName];

            // 没有设置DLL的名称 //
            if (strDLL == null || strDLL.Length == 0)
                return null;

            // 没有设置Type的名称 //
            if (TypeName == null || TypeName.Length == 0)
                return null;

            if (strDLL.StartsWith("~/"))
            {
                // 获得真实的DLL地址 //
                strDLL = GetRootPath() + "\\" + strDLL.Substring(2);
            }

            if (!File.Exists(strDLL))
            {
                // 如果DLL文件不存在 //
                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "文件(" + strDLL + ")不存在。"

                    );
            }

            Assembly ass = Assembly.LoadFile(strDLL);

            Type theType = ass.GetType(TypeName);

            if (theType == null)
            {
                throw new Exception
                    (
                        "方法:"
                        + MethodBase.GetCurrentMethod().ReflectedType.FullName
                        + " "
                        + MethodBase.GetCurrentMethod().ToString()
                        + " 发生异常:"
                        + "反射没有找到类(" + TypeName + ")"

                    );
            }

            // 反射创建实体 //
            return ass.CreateInstance(theType.FullName) as IDataBaseHelper;
        }


        /// <summary>
        /// 通过反射创建类对象 Sqlite相关的
        /// </summary>
        protected static IDataBaseHelper m_SQLiteHelper
            =
            SQLiteHelperCreate();


标签:通过反射创建Sqlite的数据库操作实体 

上一条:

下一条:


 

相关评论

评论加载中……
 

发表评论

类型:
内容:
  (Alt+Enter)