Type.GetType(string typeName) 返回 null

发布于 2024-12-04 15:15:03 字数 1792 浏览 4 评论 0原文

我的代码是

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

骄傲 2024-12-11 15:15:03

如果您知道它是什么类型都将在 DataAccessLayer 内,那么我会尽可能简单地获得 Assembly 引用,例如,

 Assembly assembly = typeof(AnyPublicTypeWithinTargetAssembly).Assembly;
 Type type = assembly.GetType(namespaceQualifiedTypeName);

另一种方法是使用 Type .GetType 具有程序集限定名称,但在指定类型名称方面更加冗长。

If you know that whatever type it is will be within DataAccessLayer, then I'd get an Assembly reference as simply as possible, e.g.

 Assembly assembly = typeof(AnyPublicTypeWithinTargetAssembly).Assembly;
 Type type = assembly.GetType(namespaceQualifiedTypeName);

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.

眉黛浅 2024-12-11 15:15:03

如果调用程序集中不存在该类型,则需要使用 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 set key value with AssemblyQualifiedName instead of namespace qualified name.

时光病人 2024-12-11 15:15:03

如果 CExamDAOExamDao 的子类,则表示法为(注意 +):

Catalyst.DAL.ExamDAO+CExamDAO

您能做的最好的事情就是直接创建一个 CExamDAO然后获取其 GetType().AssemblyQualifiedName (例如在调试器中)。类似:

(new CExamDAO()).GetType().AssemblyQualifiedName

或(如果您确定需要它的位置,其程序集已加载)

(new CExamDAO()).GetType().FullName

,然后将其复制/粘贴到代码中。

If CExamDAO is a subclass of ExamDao, then the notation is (note the +):

Catalyst.DAL.ExamDAO+CExamDAO

The best thing you can do is create a CExamDAO directly and then take its GetType().AssemblyQualifiedName (for example in the debugger). Something like:

(new CExamDAO()).GetType().AssemblyQualifiedName

or (if you are sure where you need it its assembly is already loaded)

(new CExamDAO()).GetType().FullName

and then copy/paste it in your code.

-柠檬树下少年和吉他 2024-12-11 15:15:03

你的类型是公开的吗?
不能从不同的程序集中加载内部类。

Is your type public?
Internal classes can not be loaded from different assemblies.

予囚 2024-12-11 15:15:03

或者尝试以下操作:

 private static object GetResultFromStaticMethodClass(string qualifiedClassName, string method)
 {
      Type StaticClass = Type.GetType(qualifiedClassName);
      MethodInfo methodInfo = StaticClass.GetMethod(method);
      object result = methodInfo.Invoke(null, null);
      return result;
 }

使用:

object result = GetResultFromStaticMethodClass(
    "Utilities.StringHelper,DaProject",
    "ToList"
);

调用 DaProjectUtilities 命名空间中 StringHelper 类中的静态方法 ToList /strong> 项目(相同的程序集和项目名称)。

如果需要参数,请将它们添加到 methodInfo.Invoke(null, null) 调用的第二个参数中

Or try this:

 private static object GetResultFromStaticMethodClass(string qualifiedClassName, string method)
 {
      Type StaticClass = Type.GetType(qualifiedClassName);
      MethodInfo methodInfo = StaticClass.GetMethod(method);
      object result = methodInfo.Invoke(null, null);
      return result;
 }

Use:

object result = GetResultFromStaticMethodClass(
    "Utilities.StringHelper,DaProject",
    "ToList"
);

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文