{"record":{"id":"11d5639715742a24","repo":"pardeike/Harmony","slug":"can-not-get-il-bytes-of-method-method-fulldescription","errorCode":null,"errorMessage":"Can not get IL bytes of method {method.FullDescription()}","messagePattern":"Can not get IL bytes of method (.+?)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"Harmony/Internal/MethodCopier.cs","lineNumber":107,"sourceCode":"\t\t}\r\n\r\n\t\tinternal MethodBodyReader(MethodBase method, ILGenerator generator)\r\n\t\t{\r\n\t\t\tthis.generator = generator;\r\n\t\t\tthis.method = method;\r\n\t\t\tmodule = method.Module;\r\n\r\n\t\t\tvar body = method.GetMethodBody();\r\n\t\t\tif ((body?.GetILAsByteArray()?.Length ?? 0) == 0)\r\n\t\t\t{\r\n\t\t\t\tilBytes = new ByteBuffer([]);\r\n\t\t\t\tilInstructions = [];\r\n\t\t\t}\r\n\t\t\telse\r\n\t\t\t{\r\n\t\t\t\tvar bytes = body.GetILAsByteArray();\r\n\t\t\t\tif (bytes is null)\r\n\t\t\t\t\tthrow new ArgumentException(\"Can not get IL bytes of method \" + method.FullDescription());\r\n\t\t\t\tilBytes = new ByteBuffer(bytes);\r\n\t\t\t\tilInstructions = new List<ILInstruction>((bytes.Length + 1) / 2);\r\n\t\t\t}\r\n\r\n\t\t\tvar type = method.DeclaringType;\r\n\r\n\t\t\tif (type is not null && type.IsGenericType)\r\n\t\t\t{\r\n\t\t\t\ttry\r\n\t\t\t\t{ typeArguments = type.GetGenericArguments(); }\r\n\t\t\t\tcatch { typeArguments = null; }\r\n\t\t\t}\r\n\r\n\t\t\tif (method.IsGenericMethod)\r\n\t\t\t{\r\n\t\t\t\ttry\r\n\t\t\t\t{ methodArguments = method.GetGenericArguments(); }\r\n\t\t\t\tcatch { methodArguments = null; }\r","sourceCodeStart":89,"sourceCodeEnd":125,"githubUrl":"https://github.com/pardeike/Harmony/blob/e7872dc17008bc0ca2b3de1f53fd4120dbd7a17c/Harmony/Internal/MethodCopier.cs#L89-L125","documentation":"MethodBodyReader asks the runtime for the method's IL as a byte array (MethodBase.GetMethodBody().GetILAsByteArray()) and throws if the bytes are null. This happens for methods that have no IL body at all — abstract methods, extern/P-Invoke methods, runtime-intrinsic methods, or methods implemented natively. Harmony cannot copy or transpile a method that has no IL.","triggerScenarios":"Attempting to Patch() or reverse-patch an abstract or interface method; patching an extern or P/Invoke (DllImport) method; patching methods with MethodImplAttributes like AggressiveInlining + native/runtime on some runtimes; patching into a Mono/runtime where GetMethodBody returns null for the target.","commonSituations":"Trying to transpile interface or abstract methods instead of the concrete implementation; patching native methods (UnityEngine internal calls) which have no CIL body; targeting a property accessor that is abstract; AOT/IL2CPP environments where JIT IL bodies are unavailable.","solutions":["Patch the concrete implementation (derived type's override) instead of the abstract/interface method","Do not attempt to patch extern/P-Invoke/native methods; patch a managed caller instead or use a different hooking technique","Check method.IsAbstract / method.GetMethodBody() == null before patching and skip or warn","For interface contracts, patch all concrete implementations or use a proxy/dispatcher pattern"],"exampleFix":"// before\nvar m = typeof(IFoo).GetMethod(\"Bar\"); // abstract - no IL\nharmony.Patch(m, transpiler: trans);\n// after\nvar m = typeof(FooImpl).GetMethod(\"Bar\"); // concrete override with IL body\nif (m.GetMethodBody() != null)\n    harmony.Patch(m, transpiler: trans);","handlingStrategy":"validation","validationCode":"bool CanPatch(MethodBase m) => m is not null && !m.IsAbstract && !m.IsVirtual || (m.IsVirtual && m.GetMethodBody() is not null);\n// safer: check body directly\nbool HasIl(MethodBase m) => m?.GetMethodBody()?.GetILAsByteArray() is { Length: > 0 };","typeGuard":"static bool IsPatchable(MethodBase m) =>\n    m is MethodInfo mi && mi.GetMethodBody() != null;","tryCatchPattern":"try { harmony.Patch(target, transpiler: trans); }\ncatch (ArgumentException ex) when (ex.Message.Contains(\"Can not get IL bytes\"))\n{ Log.Warn($\"{target.FullDescription()} has no IL body (abstract/extern/native) — skipping\"); }","preventionTips":["Never target abstract, interface, or extern/P-Invoke methods; patch concrete implementations or managed callers","Check GetMethodBody() != null before transpiling","On IL2CPP/AOT, verify the method is actually JIT-compiled IL before patching","Prefer patching a managed wrapper over native internal-call methods"],"tags":["harmony","il","unsupported-method"],"backgroundTag":"unsupported-operation","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"}