typeof运算(T)与Object.GetType()的性能 [英] typeof(T) vs. Object.GetType() performance

查看:1097
本文介绍了typeof运算(T)与Object.GetType()的性能的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是任何人都知道的的typeof(T)的任何差别,其中T:结构
为例,对 t.GetType ()其中t是一个System.Object ?结果
ILDASM显示的typeof(T)使用的System.Type :: GetTypeFromHandle(手柄的RuntimeTypeHandle),另一种是只是普通的 System.Object的::的GetType()。的实现是 [MethodImpl(MethodImplOptions.InternalCall)]
所以方法在CLR本地代码定义。 ?所以,我只是想知道是否有人知道任何理由,更喜欢一个比其他

Is anyone aware of any differences between typeof(T) where T : struct, for example, vs. t.GetType() where t is a System.Object?
ILdasm shows that typeof(T) uses System.Type::GetTypeFromHandle(RuntimeTypeHandle handle), and the other is just plain System.Object::GetType(). The implementations are [MethodImpl(MethodImplOptions.InternalCall)], so the methods are defined in native code in the CLR. So, I'm just wondering if anyone is aware of any reason to prefer one over the other?

编辑:让我澄清一下,我在情况最感兴趣它似乎并不无论你选择 - 那就是,是否有性能差异,或任何其他原因?谢谢!

Let me clarify, I'm mostly interested in the cases where it doesn't seem to matter which you choose - that is, is there a performance difference, or any other reason? Thanks!

推荐答案

的typeof 使用,当你想要得到的键入代表一个特定的类型实例。 的GetType 给其所调用的对象,这可能是从声明的类型不同的运行时类型。

typeof is used when you want to get the Type instance representing a specific type. GetType gives the runtime type of the object on which it is called, which may be different from the declared type.

例如:

class A {}

class B : A {}

class Program
{
    static A CreateA()
    {
        return new B();
    }

    static void Main()
    { 
        A a = CreateA();
        Console.WriteLine(typeof(A));     // Writes "A"
        Console.WriteLine(a.GetType());   // Writes "B"
    }
}

在上述情况下,在方法中,你要处理的类型的实例 A ;因此,如果你关心声明的类型,你可以使用 typeof运算(A)。但是, CreateA 方法实际上, B ,尽管声明基类作为返回返回一个派生类的一个实例类型。如果你想了解这个运行时类型,调用的GetType 在返回的实例。

In the above case, within the Main method, you're dealing with instances of type A; thus, if you care about the declared type, you would use typeof(A). However, the CreateA method actually returns an instance of a derived class, B, despite declaring the base class as the return type. If you want to find out about this runtime type, call GetType on the returned instance.

修改:在正确的方向迈赫达德的评论点。虽然的typeof 发出 GetTypeFromHandle 调用了的RuntimeTypeHandle 作为参数,所述参数实际上对应于特定类型,其元数据标记是评价堆栈上。在一些情况下,此令牌将是有隐式(由于当前方法调用);否则,它可以有明确地通过调用 ldtoken 的推压。你可以看到更多这样的例子在这些问题的答案:

Edit: Mehrdad's comment points in the right direction. Although typeof emits a GetTypeFromHandle call that takes a RuntimeTypeHandle as parameter, the said parameter would actually correspond to the specific type whose metadata token is on the evaluation stack. In some instances, this token would be there implicitly (due to the current method invocation); otherwise, it can be pushed there explicitly by calling ldtoken. You can see more examples of this in these answers:

  • Efficiency of C#'s typeof operator (or whatever its representation is in MSIL)
  • Generating IL for 2D Arrays

修改 2 :如果你正在寻找业绩基准,你可以参考乔恩斯基特的回答。他的结果是:

typeof(Test):   2756ms
test.GetType(): 3734ms

这篇关于typeof运算(T)与Object.GetType()的性能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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