嗨,盖兹(Guyz),您能给我一个容易理解的typeof()含义吗? [英] Hi Guyz can you give me an easy to get meaning of typeof()
本文介绍了嗨,盖兹(Guyz),您能给我一个容易理解的typeof()含义吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
嗨盖兹,
我对使用Typeof()感到困惑,因为它的工作方式和用途
Google无法给我一个明确的答案
您能否给出一个易于理解的typeof();含义?
对于此示例:
hi guyz,
i''m confused of using Typeof(), on how it works and what''s for
google can''t give me a clear answer
can you give an easy-to-understand meaning of typeof();
for this example:
//where Customer in a class
typeof(Customer)
//Customer class
public class Customer
{
public string SayHello(string name)
{
return "Hello" + name + "!...";
}
}
谢谢Guyz,我将感谢您的回答!
thanks guyz i''ll appreciate ur answer!
推荐答案
^ ]
有什么不清楚的?而您想知道什么?
http://msdn.microsoft.com/en-us/library/58918ffs(v=VS.100).aspx#Y200[^]
What is not clear? And what you want to know?
不!整个主题并不容易.您应该了解System.Type
类型(由typeof
返回)和反射类型.如果您不了解此C#和.NET功能的帮助页面,则简短答案无法解决问题.这是关于适当的教育,而不仅仅是一些信息.
另外,您需要了解运行时和编译时(静态已知")类型之间的区别,这是OOP的核心.
现在,在提到了必要的先决条件之后,我将尝试进行解释.运算符typeof
对类型的标识符进行操作,因此它基于静态已知的类型返回System.Type
的实例.返回的实例允许执行类型比较并应用反射,并提取有关该类型的所有信息,例如其名称,其分类,访问修饰符,所有成员,其类型和属性,包括带有其参数的所有方法,参数的类型和传递方法.
与此相反,方法object.GetType
是实例(非静态)方法,因此它需要某些对象的实例.它返回对象的运行时类型,因此它不能与调用中使用的变量的编译时类型相同,而可以是派生类型之一.这个事实是OOP 多态性的核心.
例如:
No! The whole topic is not easy. You should understand the typeSystem.Type
(returned bytypeof
) and Reflection. If you don''t understand the help page in this C# and .NET feature, short answer cannot fix situation. It''s about appropriate education, not just about some information.
Also, you need to understand the difference between run-time and compile-time ("statically known") type, which lies in the heart of OOP.
Now, after a mentioned required prerequisites, I''ll try to explain. The operatortypeof
operates on the identifier of the type, so it returns the instance ofSystem.Type
based on statically known type. The returned instance allows to perform type comparison and apply Reflection and pull out all information about the type, such as its name, its classification, access modifiers, all members, their types and properties, including all methods with their parameters, types of parameters and methods of passing.
In contrast to that, the methodobject.GetType
is the instance (non-static) method, so it requires the instance of some object. It returns run-time type of the object, so it can be not the same type as the compile-time type of the variable used in the call but one of the derived types. This fact lies in the heart of OOP polymorphism.
Some example:
class Customer {/*...*/}
class ValuedCustomer : Customer {/*...*/}
//...
//usually, using such methods means abuse of OOP but can be useful in some special cases:
static bool IsDerivedCustomer(Customer customer) {
return typeof(Customer) == customer.GetType();
}
//...
Customer customer = new Customer();
Customer valuedCustomer = new ValuedCustomer();
if (IsDerivedCustomer(customer)) //will return false
System.Console.WriteLine("This is a base type. Learn OOP");
else
System.Console.WriteLine("This is a derived type. Learn OOP anyway");
if (valuedCustomer is Customer) //will return true
System.Console.WriteLine(
"This is a Customer;" +
"this method helps to check if an instance belongs to the class hierarchy");
这篇关于嗨,盖兹(Guyz),您能给我一个容易理解的typeof()含义吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文