[Unity] Type.GetType() not working?

If GetType() function return null, need to load all assemblies types and check type name.

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;
}
}

發佈留言