62,046
社区成员
发帖
与我相关
我的任务
分享
private static ConstructorInfo _CreateDomain;
static public IDomain GetDALInstance()
{
if (_CreateDomain == null)
{
var providerName = ConfigurationManager.AppSettings["DALAssemblyName"];
if (providerName == null)
throw new Exception("需要设置配置参数DALAssemblyName。");
var asm = Assembly.Load(providerName);
var className = ConfigurationManager.AppSettings["DALClass"];
if (className == null)
throw new Exception("需要设置配置参数DALClass。");
var t = asm.GetType(className);
if (t == null)
throw new Exception(string.Format("Assembly {0} 中不存在类 {1}。", asm.FullName, className));
_CreateDomain = t.GetConstructor(new Type[] { });
if (_CreateDomain == null)
throw new Exception("DALClass参数无法实例化对象。");
}
return (IDomain)_CreateDomain.Invoke(null);
}
注意是 Assembly.Load(...) 而不是 LoadFrom 更不是 LoadFile。代码没有问题,如果不能Load,你就可以查看 Assembly.Load 的具体文档,例如看看 msdn,看看你从 config 文件中提取的 DALAssemblyName 值是否符合文档中所要求的 Load 方法需要的值。