是什么在类型的实例的GetType()的回报?实例、类型、GetType

2023-09-07 03:03:52 作者:繁华落尽,洒一地忧伤

我在一些调试碰到了这个code。

I ran across this code during some debugging.

private bool HasBaseType(Type type, out Type baseType)
{
    Type originalType = type.GetType();
    baseType = GetBaseType(type);
    return baseType != originalType;
}

我最初的想法是, type.GetType()的方法总是产生的相当于typeof运算(System.Type的第一线),这似乎真的毫无意义,我在上下文中。然而, MSDN建议该键入覆盖的GetType的版本()这将是从继承的对象。然后,在非静态的实际MSDN页面 Type.GetType() (不要与三个静态版本混淆)说,的GetType()方法返回当前的类型。无相关的解释。

My initial thought was that type.GetType() in the first line of the method would always yield the equivalent of typeof(System.Type), which appears really pointless to me in context. However, MSDN suggests that Type overrides the version of GetType() that would be inherited from Object. Then, the actual MSDN page on non-static Type.GetType() (not to be confused with the three static versions) says that the GetType() method returns "The current Type." No further relevant explanation is given.

那么,是 originalType 在上述相同的方法键入或 typeof运算(System.Type的)?我不是从文档十分肯定。如果 originalType 等同于键入,会是一个的复制的的键入,这样,如果 GetBaseType 方法改变其参数, originalType 仍然坚持相当于键入无论在 GetBaseType

So, is the value of originalType in the method above equal to type or to typeof(System.Type)? I'm not quite sure from the documentation. If originalType is equivalent to type, would it be a copy of type so that, if the GetBaseType method alters its parameter, originalType would still be equivalent to type in its original form no matter what happens inside GetBaseType?

推荐答案

1。

的GetType没有被定义为虚拟并且因此不能被覆写。 System.Type的不覆盖对象的GetType,它隐藏它(在C#将它重新psented使用new关键字$ P $)。如果使用反汇编工具,如ILSpy,你会看到,它的实现原理如下:

GetType is not defined as virtual and therefore cannot be overriden. System.Type doesn't override object's GetType, it hides it (in C# it would be represented with the new keyword). If you use a disassembly tool such as ILSpy, you'll see that it's implemented as follows:

public new Type GetType()
{
    return base.GetType();
}

正如你所看到的只是调用基类的GetType(即对象)的实现,因此最终的结果是一样的。这个版本的GetType提供了一个正式实施的_Type接口。您通常不必担心这个接口,它的存在的互操作性的原因。

As you can see it only calls the implementation of GetType of the base class (which is object), therefore the end result is the same. This version of GetType provides a formal implementation to the _Type interface. You typically don't have to worry about this interface, it's there for interoperability reasons.

2。

GetType方法是定义在对象,并返回System.Type的。但仔细看看System.Type的发现,它的定义为抽象的,因此它不能被实例化。因此,一种从的GetType得到的东西具体是获得另一种类型,它派生自的System.Type。我们得到确实的是System.RuntimeType,这源于的System.Type的一个实例。

The GetType method is defined in object and returns System.Type. But a closer look at System.Type reveals that it's defined as abstract, hence it can never be instantiated. So the way to get something concrete from GetType is to get another type, which derives from System.Type. And indeed what we get is an instance of System.RuntimeType, which derives from System.Type.

这其中的原因是,必须是重新presents给定类型只有一个实例。也就是说,它并不重要字符串有多少不同的实例(例如)调用的GetType,你还是会得到完全相同的情况下,因为有一个描述字符串只有一个实例。为了确保只有一个实例,System.Type的未被定义为抽象的,因此它不能被用户code实例化并System.RuntimeType定义为内部的,所以它的唯一访问内的mscorlib code和也可以用户code被实例化。

The reason for that is that there must be only one instance that represents a given type. That is, it doesn't matter how many different instances of string (for example) you invoke GetType on, you'll still get the exact same instance, because there is only one instance that describes a string. To ensure that there is only one instance, System.Type was defined as abstract so it cannot be instantiated by user code and System.RuntimeType was defined as internal, so it's only accessible to code within mscorlib and also cannot be instantiated by user code.

3。

System.RuntimeType的存在是一个实现细节可能在大多数情况下被忽略。对于所有意图和目的,你的code可以假设的GetType返回的System.Type的一个实例。 唯一的例外是与以下code(你可以用任何其他类型的替换字符串):

The existence of System.RuntimeType is an implementation detail that may be in most cases ignored. For all intents and purposes your code can assume that GetType returns an instance of System.Type. The only exception is with the following code (you can replace string with any other type):

bool b = 
    typeof(string).GetType() == typeof(Type);

如果不知道System.RuntimeType中,人们会认为,因为它假定的GetType返回System.Type的,这显然是右手EX pression的值,B。将如此。但是由于左手值实际上System.RuntimeType,b为假(System.Type的和System.RuntimeType两种不同的类型,因此是不相等的)。这是你需要注意System.RuntimeType的理解的唯一案例,为什么因为它的code的行为。在任何其他情况下,它只是不要紧的GetType返回System.RuntimeType,你可以忽略它。

If unaware of System.RuntimeType, one would assume that b would be true because it's assumed that GetType returns System.Type, which is clearly the value of the right hand expression. But since the left hand value is in fact System.RuntimeType, b is false (System.Type and System.RuntimeType are two different types and therefore aren't equal). This is the only case in which you need to be aware of System.RuntimeType to understand why the code behave as it does. In any other case, it just doesn't matter that GetType returns System.RuntimeType and you can ignore it.

4。

下面是一个直接链接到我的.NET在线课程,讨论运行时类型信息(RTTI)的一部分。我不提System.RuntimeType,因为就像我说这是一个实现细节可以在很大程度上被忽略。但这个链接会给你更多的背景和的System.Type的更清晰的认识。

Here is a direct link to the part of my .NET online course that discusses Runtime Type Information (RTTI). I don't mention System.RuntimeType because like I said it's an implementation detail that can for the most part be ignored. But this link will give you more background and clearer understanding of System.Type.

http://motti.me/tw

我希望这有助于!

Motti