What does GetType() return on an instance of Type?

前端 未结 3 1039
无人及你
无人及你 2021-01-24 07:52

I ran across this code during some debugging.

private bool HasBaseType(Type type, out Type baseType)
{
    Type originalType = type.GetType();
    baseType = Get         


        
3条回答
  •  滥情空心
    2021-01-24 08:25

    It looks like neither. When you call GetType on a Type instance, it seems to always return RuntimeType.

    I checked this against the System assembly by running this code snippet:

    Assembly
      .GetAssembly(typeof(int))
      .GetTypes()
      .Where(type => type.GetType() == type)
    

    Edit: As Luke clarified in comments, the "original type" is only actually the original type when given a RuntimeType - it doesn't work for anything else. And since that type is internal to system, the method seems simply wrong.

提交回复
热议问题