• typeof和GetType的区别


    http://stackoverflow.com/questions/4537945/what-is-the-difference-of-getting-type-by-using-gettype-and-typeof

    You can only use typeof() when you know that type at compile time, and you're trying to obtain the corresponding Type object. (Although the type could be a generic type parameter, e.g. typeof(T) within a class with a type parameter T.) There don't need to be any instances of that type available to use typeof. The operand for typeof is always the name of a type or type parameter. It can't be a variable or anything like that.

    Now compare that with object.GetType(). That will get the actual type of the object it's called on. This means:

    • You don't need to know the type at compile time (and usually you don't)
    • You do need there to be an instance of the type (as otherwise you have nothing to call GetType on)
    • The actual type doesn't need to be accessible to your code - for example, it could be an internal type in a different assembly

    One odd point: GetType will give unexpected answers on nullable value types due to the way that boxing works. A call to GetType will always involve boxing any value type, including a nullable value type, and the boxed value of a nullable value type is either a null reference or a reference to an instance of a non-nullable value type.

  • 相关阅读:
    攻防世界 xff_referer
    攻防世界 simple——js
    攻防世界 你知道什么是cookie吗?
    攻防世界 你知道php备份文件吗?
    攻防世界 robots题
    bugku 管理员系统
    bugku 速度要快
    2016北京集训测试赛(七)Problem A: 自动机
    BZOJ 4543 2016北京集训测试赛(二)Problem B: thr
    BZOJ 4631 踩气球
  • 原文地址:https://www.cnblogs.com/chucklu/p/4533389.html
Copyright © 2020-2023  润新知