mirror of
https://github.com/sinai-dev/UnityExplorer.git
synced 2025-06-28 10:52:44 +08:00
Faster non-generic Il2Cpp casting
This commit is contained in:
@ -22,15 +22,19 @@ namespace Explorer
|
|||||||
public static ILType ComponentType => Il2CppType.Of<Component>();
|
public static ILType ComponentType => Il2CppType.Of<Component>();
|
||||||
public static ILType BehaviourType => Il2CppType.Of<Behaviour>();
|
public static ILType BehaviourType => Il2CppType.Of<Behaviour>();
|
||||||
|
|
||||||
private static readonly MethodInfo m_tryCastMethodInfo = typeof(Il2CppObjectBase).GetMethod("TryCast");
|
private static readonly MethodInfo tryCastMethodInfo = typeof(Il2CppObjectBase).GetMethod("TryCast");
|
||||||
|
private static readonly Dictionary<Type, MethodInfo> cachedTryCastMethods = new Dictionary<Type, MethodInfo>();
|
||||||
|
|
||||||
public static object Il2CppCast(object obj, Type castTo)
|
public static object Il2CppCast(object obj, Type castTo)
|
||||||
{
|
{
|
||||||
if (!typeof(Il2CppSystem.Object).IsAssignableFrom(castTo)) return obj;
|
if (!typeof(Il2CppSystem.Object).IsAssignableFrom(castTo)) return obj;
|
||||||
|
|
||||||
return m_tryCastMethodInfo
|
if (!cachedTryCastMethods.ContainsKey(castTo))
|
||||||
.MakeGenericMethod(castTo)
|
{
|
||||||
.Invoke(obj, null);
|
cachedTryCastMethods.Add(castTo, tryCastMethodInfo.MakeGenericMethod(castTo));
|
||||||
|
}
|
||||||
|
|
||||||
|
return cachedTryCastMethods[castTo].Invoke(obj, null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static bool IsEnumerable(Type t)
|
public static bool IsEnumerable(Type t)
|
||||||
|
Reference in New Issue
Block a user