Home > Net >  C# System.Type.GetType(string) returns nested class instead of the global class
C# System.Type.GetType(string) returns nested class instead of the global class

Time:07-26

I have the following code structure as example:

public class Character {
    public enum MovementType {
    }
}

public class CombatMode {
    public class FinalFight {
        public class Character {
        }
    }
}

The problem is System.Type.GetType("Character") will return any Nested class named Character instead of global::Character.

Examples:

// This returns "Character, Assembly-CSharp, Version=0.0.0.0, Culture=neutral, PublicKeyToken=null" which is correct
string characterClassTypeName = typeof(global::Character).AssemblyQualifiedName;

// This will return the type CombatMode.FinalFight.Character and NOT global::Character like I would expect
System.Type.GetType(characterClassTypeName);
// Same with this
System.Type.GetType("Character");
// This returns null instead of the nested type Character.MovementType
System.Type.GetType("Character MovementType");
// Same with this
System.Type.GetType(typeof(Character.MovementType).AssemblyQualifiedName);

What is the proper way to get the type global::Character from a string?

For context I am running this code in Unity 2021 TLS IL2CPP build. This issue does not occur in Unity editor or in Unity 2019 TLS IL2CPP build. So I am wondering if it is something with GetType I don't understand or maybe a new bug with IL2CPP?

CodePudding user response:

It was a IL2CPP bug which was already reported, and it will be fixed in 2021.3.7.

  • Related