Skip to content

Commit

Permalink
Handle unboxing for generic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ds5678 committed Sep 15, 2024
1 parent 1872123 commit 3950547
Showing 1 changed file with 5 additions and 1 deletion.
6 changes: 5 additions & 1 deletion Il2CppInterop.Generator/Utils/UnstripTranslator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>().
var newInstruction = targetBuilder.Add(OpCodes.Call,
imports.Module.DefaultImporter.ImportMethod(imports.Il2CppObjectBase_Cast.Value.MakeGenericInstanceMethod(targetType)));
instructionMap.Add(bodyInstruction, newInstruction);
Expand Down

0 comments on commit 3950547

Please sign in to comment.