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

查看:18
本文介绍了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.RuntimeTypeSystem.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() 而不是直接使用该对象时,可能会出现混淆.然后 Type.ToString() 将返回 "System.RuntimeType" 当它被调用的对象代表一个类型时:

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() 将返回另一个表示 System.RuntimeTime 类型的 Type 对象,因为这是用于表示原始类型对象的具体类型.博文然后展示了一些不必要的反射技巧,以获取原始类型对象的类型,而实际上所需的只是:

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;

因此,如果您的 Type 对象报告它代表一个 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天全站免登陆