日期:2014-05-18 浏览次数:21081 次
// Assembly加载程序集,读取程序集
Assembly _assembly = Assembly.Load("你DLL的名字");
// 获取程序集中的类
IBaseCommand iCmd = null;
foreach (Type type in _assembly.GetTypes())
{
if (type.FullName.StartsWith("类所在的名命空间,如以System.Windows开头"))
{
iCmd = (IBaseCommand)Activator.CreateInstance(type);
if (iCmd.ChineseCommand.Equals(commandName) || iCmd.EnglishCommand.Equals(commandName.ToLower()))
{
iCmd.Execution();
break;
}
}
}
------解决方案--------------------
有现成的,不过看了一些微软的经典项目,他们好像都是直接调用数据访问层。
//path需要手动修改,要反射的项目名称,三层里面的单个项目名称,如MyBookDal。
private static readonly string DBType =
System.Configuration.ConfigurationManager.AppSettings["dbtype"];
/// <summary>
/// 反射创建类的实例
/// </summary>
/// <typeparam name="T">类名称或者类继承的接口</typeparam>
/// <param name="className">类名称</param>
/// <returns>类的事例</returns>
public static T CreateOjectDal<T>(string className)
{
className = DBType + "." + className;
//找程序集-->实例化类--> 转为父类
return (T)Assembly.Load(DBType).CreateInstance(className);
}