C# 中的 myCustomer.GetType() 和 typeof(Customer) 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/139607/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between myCustomer.GetType() and typeof(Customer) in C#?
提问by user18931
I've seen both done in some code I'm maintaining, but don't know the difference. Is there one?
我已经在我维护的一些代码中看到两者都完成了,但不知道有什么区别。有吗?
let me add that myCustomer is an instance of Customer
让我补充一点,myCustomer 是 Customer 的一个实例
采纳答案by Kilhoffer
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
.
在您的情况下,两者的结果完全相同。它将是您的自定义类型,它源自System.Type
. 这里唯一真正的区别是,当您想从类的实例中获取类型时,您使用GetType
. 如果您没有实例,但您知道类型名称(并且只需要实际System.Type
检查或比较),您将使用typeof
.
Important difference
重要区别
EDIT: Let me add that the call to GetType
gets resolved at runtime, while typeof
is resolved at compile time.
编辑:让我补充一点,调用GetType
在运行时得到解决,而typeof
在编译时得到解决。
回答by Chris Ballard
For the first, you need an actual instance (ie myCustomer), for the second you don't
对于第一个,您需要一个实际实例(即 myCustomer),第二个则不需要
回答by Jakub ?turc
Yes, there is a difference if you have an inherited type from Customer.
是的,如果您从 Customer 继承了类型,则有所不同。
class VipCustomer : Customer
{
.....
}
static void Main()
{
Customer c = new VipCustomer();
c.GetType(); // returns typeof(VipCustomer)
}
回答by Jeffrey L Whitledge
GetType() is used to find the actualtype of a object reference at run-time. This can be different from the type of the variable that references the object, because of inheritance. typeof() creates a Type literal that is of the exact type specified and is determined at compile-time.
GetType() 用于在运行时查找对象引用的实际类型。由于继承的原因,这可能与引用对象的变量的类型不同。typeof() 创建一个 Type 字面量,该字面量具有指定的确切类型并在编译时确定。
回答by FlySwat
typeof(foo) is converted into a constant during compiletime. foo.GetType() happens at runtime.
typeof(foo) 在编译时被转换成一个常量。foo.GetType() 在运行时发生。
typeof(foo) also converts directly into a constant of its type (ie foo), so doing this would fail:
typeof(foo) 也直接转换为它的类型的常量(即 foo),所以这样做会失败:
public class foo
{
}
public class bar : foo
{
}
bar myBar = new bar();
// Would fail, even though bar is a child of foo.
if (myBar.getType == typeof(foo))
// However this Would work
if (myBar is foo)
回答by David Pokluda
typeof is executed at compile time while GetType at runtime. That's what is so different about these two methods. That's why when you deal with type hierarchy, you can find out the exact type name of a type simply by running GetType.
typeof 在编译时执行,而 GetType 在运行时执行。这就是这两种方法的不同之处。这就是为什么在处理类型层次结构时,只需运行 GetType 即可找出类型的确切类型名称。
public Type WhoAreYou(Base base)
{
base.GetType();
}
回答by Praveen Kumar
The typeof operator takes a type as a parameter. It is resolved at compile time. The GetType method is invoked on an object and is resolved at run time. The first is used when you need to use a known Type, the second is to get the type of an object when you don't know what it is.
typeof 运算符将类型作为参数。它在编译时解决。GetType 方法在对象上调用并在运行时解析。第一个在需要使用已知类型时使用,第二个是在不知道对象的类型时获取它的类型。
class BaseClass
{ }
class DerivedClass : BaseClass
{ }
class FinalClass
{
static void RevealType(BaseClass baseCla)
{
Console.WriteLine(typeof(BaseClass)); // compile time
Console.WriteLine(baseCla.GetType()); // run time
}
static void Main(string[] str)
{
RevealType(new BaseClass());
Console.ReadLine();
}
}
// ********* By Praveen Kumar Srivastava