Kun*_*esh 6 c# reflection class activator gettype
我的代码是
type = Type.GetType(key);
Run Code Online (Sandbox Code Playgroud)
我传递的密钥是名称空间限定名称.
我的代码在BusinessLayer中.我正在创建DataAccessLayer的实例.DataAccessLayer引用已添加到BusinessLayer.
我收到错误为"无法加载类型'Catalyst.DAL.ExamDAO.CExamDAO'来自程序集'BusinessLayer,版本= 1.9.3.0,文化=中立,公开密钥=空'." .
我该怎么做才明确指出该类来自DataAccessLayer?
关键问题是"Catalyst.DAL.ExamDAO.CExamDAO"
编辑:
我的实际代码是
public static object getClassInstance(string key, params object[] constructorArgs)
{
string assemblyPath = null;
string customClassName = null;
DataSet objDataset = getAssemblyInfo(key);
if (objDataset != null && objDataset.Tables.Count > 0 && objDataset.Tables[0].Rows.Count > 0)
{
assemblyPath = objDataset.Tables[0].Rows[0]["ACA_ASSEMBLY_PATH"].ToString();
customClassName = objDataset.Tables[0].Rows[0]["ACA_CLASS_NAME"].ToString();
}
Assembly assembly;
Type type;
if (assemblyPath != null && assemblyPath != string.Empty)
{
assembly = Assembly.LoadFile(assemblyPath);
type = assembly.GetType(customClassName);
}
else // if no customisation
{
type = Type.GetType(key);
}
object classInstance = constructorArgs == null ? Activator.CreateInstance(type) : Activator.CreateInstance(type, constructorArgs);
if (classInstance == null) throw new Exception("broke");
return classInstance;
}
Run Code Online (Sandbox Code Playgroud)
如果没有自定义,我正在尝试加载默认类.方法在BO中.如果我将密钥作为任何Bo类型的名称空间限定名称传递,它将转换.但DAO类型不会
如果您知道无论它是什么类型都将在 之内DataAccessLayer,那么我会Assembly尽可能简单地获得参考,例如
Assembly assembly = typeof(AnyPublicTypeWithinTargetAssembly).Assembly;
Type type = assembly.GetType(namespaceQualifiedTypeName);
Run Code Online (Sandbox Code Playgroud)
另一种方法是使用Type.GetType程序集限定名称,但这在指定类型名称方面更为冗长。