类型检查:typeof运算,的GetType,或者是什么? [英] Type Checking: typeof, GetType, or is?

查看:116
本文介绍了类型检查:typeof运算,的GetType,或者是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我见过很多人用下面的code:

 键入T = typeof运算(OBJ1);
如果(T == typeof运算(INT))
    //有些code在这里

但我知道你也可以做到这一点:

 如果(obj1.GetType()== typeof运算(INT))
    //有些code在这里

或者这样:

 如果(OBJ1为int)
    //有些code在这里

就个人而言,我觉得最后一个是最干净的,但有我丢失的东西?哪一个是最好用,或者是个人的preference?


解决方案

所有的都不同。


  • 的typeof 需要一个类型名称(这在编译时注明)。

  • 的GetType 得到一个实例的运行时类型。

  • 如果一个实例是在继承树返回true。

示例

 类动物{}
Dog类:动物{}空隙PrintTypes(动物一){
    打印(a.GetType()== typeof运算(动物))//假
    打印(一个是动物)//真
    打印(a.GetType()== typeof运算(狗))//真
}狗现货=新的狗();
PrintTypes(现货);



  

关于的typeof(T)什么?难道也是在编译时解决?


是的。 T是总的前pression的类型是什么。请记住,一个通用的方法基本上是一大堆用适当类型的方法。例如:

 字符串美孚< T>(T对象){返回的typeof(T).Name点; }动物probably_a_dog =新的狗();
狗definitely_a_dog =新的狗();美孚(probably_a_dog); //这个调用foo<动物>并返回动物
美孚<动物>(probably_a_dog); //这正是与上述相同
美孚<狗和GT;(probably_a_dog); //!这不会编译。该参数期望一只狗,你不能在一个动物通过。美孚(def​​initely_a_dog); //这个调用foo<狗和GT;并返回犬
美孚<狗和GT;(definitely_a_dog); //这是完全与上述相同。
美孚<动物>(definitely_a_dog); //这个调用foo<动物>并返回动物。
美孚((动物)definitely_a_dog); //这并不与上面相同,则返回动物

I've seen many people use the following code:

Type t = typeof(obj1);
if (t == typeof(int))
    // Some code here

But I know you could also do this:

if (obj1.GetType() == typeof(int))
    // Some code here

Or this:

if (obj1 is int)
    // Some code here

Personally, I feel the last one is the cleanest, but is there something I'm missing? Which one is the best to use, or is it personal preference?

解决方案

All are different.

  • typeof takes a type name (which you specify at compile time).
  • GetType gets the runtime type of an instance.
  • is returns true if an instance is in the inheritance tree.

Example

class Animal { } 
class Dog : Animal { }

void PrintTypes(Animal a) { 
    print(a.GetType() == typeof(Animal)) // false 
    print(a is Animal)                   // true 
    print(a.GetType() == typeof(Dog))    // true
}

Dog spot = new Dog(); 
PrintTypes(spot);


What about typeof(T)? Is it also resolved at compile time?

Yes. T is always what the type of the expression is. Remember, a generic method is basically a whole bunch of methods with the appropriate type. Example:

string Foo<T>(T object) { return typeof(T).Name; }

Animal probably_a_dog = new Dog();
Dog    definitely_a_dog = new Dog();

Foo(probably_a_dog); // this calls Foo<Animal> and returns "Animal"
Foo<Animal>(probably_a_dog); // this is exactly the same as above
Foo<Dog>(probably_a_dog); // !!! This will not compile. The parameter expects a Dog, you cannot pass in an Animal.

Foo(definitely_a_dog); // this calls Foo<Dog> and returns "Dog"
Foo<Dog>(definitely_a_dog); // this is exactly the same as above.
Foo<Animal>(definitely_a_dog); // this calls Foo<Animal> and returns "Animal". 
Foo((Animal)definitely_a_dog); // this does the same as above, returns "Animal"

这篇关于类型检查:typeof运算,的GetType,或者是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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