是什么在C#中的System.Type和System.RuntimeType之间的区别? [英] What's the difference between System.Type and System.RuntimeType in C#?

查看:1768
本文介绍了是什么在C#中的System.Type和System.RuntimeType之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是想今天做一些约定测试,并在装配让所有的类型(通过调用 Assembly.GetTypes()),当我迷迷糊糊到的东西:

I was trying to do some convention tests today, and getting all the types in an assembly (by calling Assembly.GetTypes()), when I stumbled into something:

System.RuntimeType:[First.Namespace.FirstClass]

每当我试着来比较同类型 typeof运算(的Firstclass),他们是不相等的。所以,当我试图找到所有包含的Firstclass 作为泛型参数,我没有找到任何类型的。

Whenever I try to compare that type with typeof(FirstClass), they're not equal. So, when I try to find all the types that contain FirstClass as a generic parameter, I'm not finding any.

这有什么区别 System.RuntimeType 的System.Type

有什么办法来解决我的问题?

Is there any way to solve my problem?

推荐答案

System.RuntimeType 是从抽象基类派生的具体类 System.Type的。由于 System.RuntimeType 不公开,你会通常会遇到它的实例为的System.Type

System.RuntimeType is a concrete class that derives from the abstract base class System.Type. Since System.RuntimeType is not public, you will typically encounter instances of it as System.Type.

混乱,可能会出现,当你试图让一个对象的类型,并错误地调用的GetType()另一个对象重新presenting第一个对象的类型,而不仅仅是直接使用该对象。然后 Type.ToString()将返回System.RuntimeType时,它被称为上的对象重新presenting一个类型:

Confusion can arise when you are trying to get the type of an object and mistakenly call GetType() on another object representing the first object's type, rather than just using that object directly. Then Type.ToString() will return "System.RuntimeType" when the object it is called on is representing a Type:

string str = string.Empty;
Type strType = str.GetType();
Type strTypeType = strType.GetType();
strType.ToString();     // returns "System.string"
strTypeType.ToString(); // returns "System.RuntimeType"

例如,在这个博客帖子有人试图获得的类型在数据库中的一列,做这样的事情:

For example, in this blog post someone is trying to get the type of a column in a database, doing something like this:

object val = reader.GetFieldType(index);
Type runtimeType = val.GetType();
PropertyInfo propInfo = runtimeType.GetProperty("UnderlyingSystemType");
Type type = (Type)propInfo.GetValue(val, null);

由于VAL已经是一个Type对象,val.GetType()将返回另一种类型的对象重新presenting类型 System.RuntimeTime ,因为这是具体类型用于重新present原始类型的对象。然后,博客文章显示了一些不必要的反射挂羊头卖狗肉,以获​​得原始类型对象的类型,真的一切都需要的时候,这是:

Since val is already a Type object, val.GetType() will return another Type object representing the type System.RuntimeTime as this is the concrete type used to represent the original type object. The blog post then shows some unnecessary reflection trickery, to get the type of the original type object, when really all that was required was:

Type type = reader.GetFieldType(index) as Type;

所以,如果你的键入对象报告,它重新presents一个 System.RuntimeType ,确保你不小心叫的GetType()您已经有了一个类型。

So if your Type object is reporting that it represents a System.RuntimeType, make sure you have not accidentally called GetType() on a type you have already got.

这篇关于是什么在C#中的System.Type和System.RuntimeType之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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