{"record":{"id":"e5eb24461f4622e1","repo":"pardeike/Harmony","slug":"wrong-emit-argument-type-operand-gettype-in-codeinstruction","errorCode":null,"errorMessage":"Wrong Emit argument type {operand.GetType()} in {codeInstruction}","messagePattern":"Wrong Emit argument type (.+?) in (.+?)","errorType":"exception","errorClass":"Exception","httpStatus":null,"severity":"error","filePath":"Harmony/Internal/MethodCreatorTools.cs","lineNumber":636,"sourceCode":"\r\n\t\t\t\t// start all exception blocks\r\n\t\t\t\tcodeInstruction.blocks.Do(block => emitter.MarkBlockBefore(block, out var _));\r\n\r\n\t\t\t\tvar code = codeInstruction.opcode;\r\n\t\t\t\tvar operand = codeInstruction.operand;\r\n\r\n\t\t\t\tswitch (code.OperandType)\r\n\t\t\t\t{\r\n\t\t\t\t\tcase OperandType.InlineNone:\r\n\t\t\t\t\t\tif (codeInstruction.IsAnnotation() == null)\r\n\t\t\t\t\t\t\temitter.Emit(code);\r\n\t\t\t\t\t\tbreak;\r\n\r\n\t\t\t\t\tcase OperandType.InlineSig:\r\n\t\t\t\t\t\tif (operand is null)\r\n\t\t\t\t\t\t\tthrow new Exception($\"Wrong null argument: {codeInstruction}\");\r\n\t\t\t\t\t\tif ((operand is ICallSiteGenerator) is false)\r\n\t\t\t\t\t\t\tthrow new Exception($\"Wrong Emit argument type {operand.GetType()} in {codeInstruction}\");\r\n\t\t\t\t\t\temitter.Emit(code, (ICallSiteGenerator)operand);\r\n\t\t\t\t\t\tbreak;\r\n\r\n\t\t\t\t\tdefault:\r\n\t\t\t\t\t\tif (operand is null)\r\n\t\t\t\t\t\t\tthrow new Exception($\"Wrong null argument: {codeInstruction}\");\r\n\t\t\t\t\t\temitter.DynEmit(code, operand);\r\n\t\t\t\t\t\tbreak;\r\n\t\t\t\t}\r\n\r\n\t\t\t\tcodeInstruction.blocks.Do(block => emitter.MarkBlockAfter(block));\r\n\t\t\t});\r\n\t\t}\r\n\r\n\t\tstatic List<CodeInstruction> InitializeOutParameter(int argIndex, Type type)\r\n\t\t{\r\n\t\t\tvar codes = new List<CodeInstruction>();\r\n\t\t\tif (type.IsByRef)\r","sourceCodeStart":618,"sourceCodeEnd":654,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Internal/MethodCreatorTools.cs#L618-L654","documentation":"For InlineSig (calli) instructions Harmony requires the operand to implement ICallSiteGenerator. If the operand is non-null but of another type (e.g. a SignatureHelper from a different emitter abstraction or a random object), EmitCodes throws this instead of silently emitting broken IL.","triggerScenarios":"A transpiler assigns a wrong-typed operand to a calli instruction — for example a SignatureHelper created for a different IL generator, a byte array, or a MethodInfo — and the wrapper is emitted.","commonSituations":"Reusing transpiler code across IL emitter versions, copying calli operands from a MethodInfo body reader into CodeInstruction without converting, or mixing Harmony versions where the signature type differs.","solutions":["Ensure the calli operand is created via the same IL generator's SignatureHelper/ICallSiteGenerator mechanism.","Rebuild the signature for the calli site instead of copying the operand from a read body.","Check that all Harmony patching assemblies reference the same Harmony version."],"exampleFix":"// before\ninstr.operand = someMethodInfo; // wrong type for calli\n\n// after\ninstr.operand = il.CreateSignature(...); // proper ICallSiteGenerator signature","handlingStrategy":"type-guard","validationCode":"foreach (var ci in transpiledInstructions)\n    if (ci.opcode.OperandType == OperandType.InlineSig && ci.operand is not null && ci.operand is not ICallSiteGenerator)\n        throw new InvalidOperationException($\"Bad calli operand type {ci.operand.GetType()}\");","typeGuard":"static bool IsCallSiteOperand(object operand) => operand is ICallSiteGenerator;","tryCatchPattern":"try { harmony.Patch(original, transpiler: t); }\ncatch (Exception ex) when (ex.Message.Contains(\"Wrong Emit argument type\"))\n{ Logger.Error($\"Bad calli operand: {ex.Message}\"); }","preventionTips":["Ensure transpiler code targets the same Harmony version across all assemblies.","Do not copy operands from body readers into calli instructions; regenerate signatures.","Add unit tests running every transpiler through a real Patch/PatchAll call."],"tags":["harmony","transpiler","type-mismatch","il-emission"],"backgroundTag":"type-mismatch","analyzedSha":"e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c","analyzedAt":"2026-09-15T22:47:11.550Z","contentChangedAt":"2026-09-15T22:47:11.550Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}