[Unity] Type.GetType() not working?



Overview

If GetType() function return null, need to load all assemblies types and check type name. Simply write a helper to use:

var type = ReflectionHelper.GetType(typeName);

public class ReflectionHelper
{
    public static Type GetType(string name)
    {
        var targetType = Type.GetType(name);
        if (targetType != null)
        {
            return targetType;
        }

        foreach (var assembly in AppDomain.CurrentDomain.GetAssemblies())
        {
            foreach (var type in assembly.GetTypes())
            {
                if (type.Name == name)
                {
                    return type;
                }
            }
        }

        return null;
    }
}



歡迎您留言與分享!(Welcome for comments or sharing!)

Related Post:

Tagged on: