代码之家  ›  专栏  ›  技术社区  ›  Matt Mitchell

“typeof”是如何工作的?

  •  14
  • Matt Mitchell  · 技术社区  · 14 年前

    我很好奇C#中typeof的“方法体”是什么样子的(我很确定我在reflector中找不到它,因为它是关键字而不是方法)。

    我猜它相当于GetType( 将符号转换为字符串 ).

    查看Reflector中的GetType(string),它调用一个方法“privategetype”,该方法调用“RuntimeTypeHandle.GetTypeByName”,RuntimeTypeHandle中似乎有很多类型背后的逻辑,但是GetTypeByName的内容并没有出现在Reflector中。

    1 回复  |  直到 11 年前
        1
  •  13
  •   Dean Harding    14 年前

    Type t = typeof(string);
    

    然后编译器编译 typeof(string) 一点一滴 ldtoken Type.GetTypeFromHandle 获取 Type 班级。

    Type.GetTypeFromHandle 类型 朋友在内部工作。。。