简体   繁体   中英

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

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM