Type.GetType(string typeName) 返回 null
我的代码是
type = Type.GetType(key);
Key 我传递的是命名空间限定名称。
我的代码在 BusinessLayer 中。我正在创建 DataAccessLayer 的实例。 DataAccessLayer 引用已添加到 BusinessLayer。
我收到的错误为“无法从程序集“BusinessLayer,Version=1.9.3.0,Culture=neutral,PublicKeyToken=null”加载类型“Catalyst.DAL.ExamDAO.CExamDAO”。”。
我应该做什么来明确指定该类来自 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;
}
如果没有自定义,我将尝试加载默认类。方法在 BO 中。 如果我将密钥作为它转换的任何 Bo 类型的命名空间限定名称传递。但 DAO 类型不会
My code is
type = Type.GetType(key);
Key which i pass is a namespace qualified name .
My code is in BusinessLayer. I am creating a instance of DataAccessLayer.
DataAccessLayer reference is added to BusinessLayer.
I am getting the error as "Could not load type 'Catalyst.DAL.ExamDAO.CExamDAO' from assembly 'BusinessLayer, Version=1.9.3.0, Culture=neutral, PublicKeyToken=null'.".
What should i do to specify explicitly thats the class is from DataAccessLayer ?
Key vale is "Catalyst.DAL.ExamDAO.CExamDAO"
Edit :
My actual code is
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;
}
I am trying to load the default classes if there is no customization . Method is in BO .
If i pass the key as namespace qualified names of any Bo type it converts . But DAO type it wont
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
如果您知道它是什么类型都将在
DataAccessLayer
内,那么我会尽可能简单地获得Assembly
引用,例如,另一种方法是使用
Type .GetType
具有程序集限定名称,但在指定类型名称方面更加冗长。If you know that whatever type it is will be within
DataAccessLayer
, then I'd get anAssembly
reference as simply as possible, e.g.An alternative is to use
Type.GetType
with an assembly-qualified name, but that's more long-winded in terms of specifying the type name.如果调用程序集中不存在该类型,则需要使用
AssemblyQualifiedName
来获取它的 Type 实例。要解决您的问题,您需要使用AssemblyQualifiedName
设置key
值,而不是命名空间限定名称
。If the type is not present in calling assembly you need to use the
AssemblyQualifiedName
to get it Type instance. To resolve your issue, you need setkey
value withAssemblyQualifiedName
instead ofnamespace qualified name
.如果
CExamDAO
是ExamDao
的子类,则表示法为(注意+
):您能做的最好的事情就是直接创建一个 CExamDAO然后获取其
GetType().AssemblyQualifiedName
(例如在调试器中)。类似:或(如果您确定需要它的位置,其程序集已加载)
,然后将其复制/粘贴到代码中。
If
CExamDAO
is a subclass ofExamDao
, then the notation is (note the+
):The best thing you can do is create a CExamDAO directly and then take its
GetType().AssemblyQualifiedName
(for example in the debugger). Something like:or (if you are sure where you need it its assembly is already loaded)
and then copy/paste it in your code.
你的类型是公开的吗?
不能从不同的程序集中加载内部类。
Is your type public?
Internal classes can not be loaded from different assemblies.
或者尝试以下操作:
使用:
调用 DaProjectUtilities 命名空间中 StringHelper 类中的静态方法 ToList /strong> 项目(相同的程序集和项目名称)。
如果需要参数,请将它们添加到 methodInfo.Invoke(null, null) 调用的第二个参数中
Or try this:
Use:
This call the static method ToList in the StringHelper class, in the Utilities namespace, in the DaProject project (same assembly and project name).
If you need parameters, add them in the second parameter in the methodInfo.Invoke(null, null) call