在C#中的Reflection中发生类型加载异常 [英] Type load exception occured in Reflection in C#

查看:96
本文介绍了在C#中的Reflection中发生类型加载异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了这段代码。我传递了类名为GetType的字符串。

类型t2 = Type.GetType(类名,true);

对象o2 =(Activator.CreateInstance(t2));





但是它给出了类型加载异常....

解决方案

首先,这是反射的目的不是使用硬编码类型名称创建某种类型的实例。从字符串中获取类型很少使用(例如,通过序列化)并且经常被滥用。但我可以理解你是否正在做一些练习,以便理解。



这些类型都没有名称class name,所以代码将会失败第一行。如果您使用其他名称,我不确定它是否是正确的全名。此外,此方法假定该类型位于调用程序集中。从你的代码中,这也是未知的。



但我们假设你得到了类型对象 t2 。然后,您正在使用的 Activator.CreateInstance 方法假定成功实例化此类型(如果应该使用该类型的无参数构造函数执行),这种情况并非总是如此。



所以,例如,这将起作用:

 命名空间 TypeNamespace {
// 我故意不使用使用指令
// 准确显示哪些类型

class 目标{
public 覆盖 string ToString(){
return 这是正确的类型;
}
}

class 计划{
静态 void Main( string [] args){
System.Type type = System.Type.GetType( TypeNamespace.Target);
System。 Object @object = System.Activator.CreateInstance(type);
// 输出这是正确的类型:
System.Console .WriteLine(@ object.ToString());
}
}
}



不是命名空间及其在文件名中的使用以及这一切都在一个程序集中。对于其他情况,您需要使用 System.Assembly 方法。



无论如何,这都是实际上没用。这不是反思的目的。



搜索类型实例化的更详细和一般方法不是 Activator ,但 System.Type 类本身。您在元数据中找到了一些合适的构造函数,如果找到这样的构造函数,则将参数传递给此方法(也可以使用 Activarot )。您需要确认是否是要激活的正确类型。永远不要依赖名字(字符串)是有意义的。一个好方法是检查类型是否实现了一些预期的接口。由于这也是类型,而不是字符串,编译器可以检查此代码的有效性。



此代码片段告诉您如何提出问题(请参阅我的评论问题)。请注意,代码示例是自包含的,专门为此讨论编写,不依赖于任何其他内容。只有这样,论坛帖子才有意义。请参阅:

http://www.sscce.org [ ^ ]。



-SA

i used this code. i passed class name which is string to GetType.
Type t2 = Type.GetType("class name",true);
Object o2 = (Activator.CreateInstance(t2));


but it gives Type load exception....

解决方案

First of all, this is not the purpose of reflection to create some instance of some type using hard-coded type name. Getting a type from string is rarely used (by serialization, for example) and fairly often abused. But I can understand if you are doing some exercises, for understanding.

None of the type can possibly have the name "class name", so the code will fail in first line. If you used another name, I'm not sure it was a correct full name. Also, this method assumes that the type is in the calling assembly. From your code, this is also unknown.

But let's assume you got the type object t2. Then the Activator.CreateInstance method you are using assumes that successful instantiation of this type if supposed to be performed using the parameterless constructor of the type, which is also not always the case.

So, this, for example, will work:

namespace TypeNamespace {
// I intentionally did not use "using" directive
// to show exactly what types are where

    class Target {
        public override string ToString() {
            return "this is the right type";
        }
    }

    class Program {
        static void Main(string[] args) {
            System.Type type = System.Type.GetType("TypeNamespace.Target");
            System.Object @object = System.Activator.CreateInstance(type);
            // outputs "this is the right type":
            System.Console.WriteLine(@object.ToString()); 
        }
    }
}


Not the the namespace and its use in the file name and the fact this is all in one assembly. For other cases, you need to use System.Assembly methods.

Anyway, this is all practically useless. This is not the purpose of reflection.

More detailed and general approach for searching of types instantiation is not Activator, but the System.Type class itself. You find some appropriate constructor in metadata and, if such constructor is found, pass arguments to this method (which can also be done with Activarot). You need to confirm is that the right type to activate. It makes sense to never rely on names (strings). One good approach is to check up if the type implements some expected interface. As this is also type, not string, the compiler can check up validity of this code.

This code fragment tells you how you should ask the questions (please see my comment to the question). Note that the code sample is self-contained, written specially for this discussion, does not depend on anything else. Only then it makes sense for a forum post. Please see:
http://www.sscce.org[^].

—SA


这篇关于在C#中的Reflection中发生类型加载异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆