DBDriverDB的驱动,通过反射获得SQLiteHelper
2017-02-21 22:44:48 访问(1462) 赞(0) 踩(0)
using System;
using System.Collections.Generic;
using System.Text;
using SlowX.IDALDBHelper;
using System.Configuration;
using System.Reflection;
using System.IO;
namespace SlowX.DAL.DLLDriver
{
/// <summary>
/// DB的驱动,通过反射获得SQLiteHelper
/// </summary>
public class DBDriver
{
/// <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>
protected static IDataBaseHelper m_SQLiteHelper = SQLiteHelperCreate();
/// <summary>
/// 通过反射创建类对象
/// </summary>
/// <returns></returns>
private static IDataBaseHelper SQLiteHelperCreate()
{
// 比如
// ~/SlowX.SQLiteX86Helper.dll
string strDLL = ConfigurationManager.AppSettings["SlowX.DAL.DLLDriver.DBDriver.SQLiteHelper.DLL"];
// SlowX.SQLiteX86Helper.DBHelper
string TypeName = ConfigurationManager.AppSettings["SlowX.DAL.DLLDriver.DBDriver.SQLiteHelper.TypeName"];
if (strDLL == null || strDLL.Length == 0)
return null;
if(TypeName==null||TypeName.Length ==0)
return null;
if (strDLL.StartsWith("~/"))
strDLL = GetRootPath() + "\\" + strDLL.Substring(2);
if (!File.Exists(strDLL))
{
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>
/// 设置SQLiteHelper
/// </summary>
/// <param name="theValue"></param>
public static void SQLiteHelperSet(IDataBaseHelper theValue)
{
m_SQLiteHelper = theValue;
}
/// <summary>
///
/// </summary>
public static IDataBaseHelper SQLiteHelper
{
get
{
return m_SQLiteHelper;
}
}
/// <summary>
///
/// </summary>
public DBDriver()
{
}
}
}
标签:
DBDriverDB的驱动,通过反射获得SQLiteHelper 


上一条:
下一条:
相关评论
发表评论