在C#中myCustomer.GetType()和typeof运算(客户)之间的区别是什么? [英] What is the difference between myCustomer.GetType() and typeof(Customer) in C#?

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

问题描述

我已经看到无论是在一些code我保持做,但不知道其中的差别。有一?

I've seen both done in some code I'm maintaining, but don't know the difference. Is there one?

让我补充一点,myCustomer是客户的一个实例

let me add that myCustomer is an instance of Customer

推荐答案

共同作用的结果正是你的情况是一样的。这将是一个派生自的System.Type 自定义类型。这里唯一的区别是,当你想从你的类的实例获得的类型,可以使用的GetType 。如果你没有一个实例,但你知道的类型名称(只需要实际的的System.Type 来检查或比较),你可以使用的typeof

The result of both are exactly the same in your case. It will be your custom type that derives from System.Type. The only real difference here is that when you want to obtain the type from an instance of your class, you use GetType. If you don't have an instance, but you know the type name (and just need the actual System.Type to inspect or compare to), you would use typeof.

编辑:让我补充一点,在调用的GetType 都在运行时解决,而的typeof 在编译解决时间。

Let me add that the call to GetType gets resolved at runtime, while typeof is resolved at compile time.

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

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