diff --git a/Il2CppInterop.Generator/Utils/UnstripTranslator.cs b/Il2CppInterop.Generator/Utils/UnstripTranslator.cs index fe1a5261..ee9ce42c 100644 --- a/Il2CppInterop.Generator/Utils/UnstripTranslator.cs +++ b/Il2CppInterop.Generator/Utils/UnstripTranslator.cs @@ -214,8 +214,12 @@ public static bool TranslateMethod(MethodDefinition original, MethodDefinition t if (targetType == null) return false; - if (bodyInstruction.OpCode == OpCodes.Castclass && !targetType.IsValueType) + if ((bodyInstruction.OpCode == OpCodes.Castclass && !targetType.IsValueType) || + (bodyInstruction.OpCode == OpCodes.Unbox_Any && targetType is GenericParameterSignature)) { + // Compilers use unbox.any for casting to generic parameter types. + // Castclass is only used for reference types. + // Both can be translated to Il2CppObjectBase.Cast(). var newInstruction = targetBuilder.Add(OpCodes.Call, imports.Module.DefaultImporter.ImportMethod(imports.Il2CppObjectBase_Cast.Value.MakeGenericInstanceMethod(targetType))); instructionMap.Add(bodyInstruction, newInstruction);