pardeike/Harmony · error · ArgumentNullException

Value cannot be null. (Parameter 'config.methodbase')

Error message

Value cannot be null. (Parameter 'config.methodbase')

What it means

The MethodCreatorConfig constructor of MethodCopier reads the source method from config.MethodBase. If the config object was built without a MethodBase, Harmony throws ArgumentNullException explicitly labelled 'config.methodbase'.

Solutions

  1. Set config.MethodBase to the source method before passing the config to MethodCopier
  2. Check your MethodCreatorConfig construction code for a missing method assignment
  3. Assert/log the config state right before constructing the copier to catch the null early
  4. If the method may be absent, resolve it defensively and fail with a clearer message of your own

Example fix

// before
var config = new MethodCreatorConfig { il = generator };
var copier = new MethodCopier(config);
// after
var config = new MethodCreatorConfig { il = generator, MethodBase = originalMethod };
var copier = new MethodCopier(config);
Defensive patterns

Strategy: validation

Validate before calling

if (config.MethodBase is null) throw new InvalidOperationException("MethodCreatorConfig.MethodBase must be set before constructing MethodCopier");

Try / catch

try { var copier = new MethodCopier(config); }
catch (ArgumentNullException ex) when (ex.ParamName == "config.methodbase") { /* set MethodBase and rebuild config */ }

Prevention

When it happens

Trigger: Calling MethodCopier(MethodCreatorConfig) where config.MethodBase is null — e.g. a MethodCreatorConfig created with only an ILGenerator/locals and never assigned the source method it should copy from.

Common situations: Programmatically generating replacement patch methods and forgetting to set the original MethodBase on the config; copying config-construction code that omitted the method assignment; refactors renaming the property so the setter call no longer binds.

Related errors


AI-assisted analysis of pardeike/Harmony@e7872dc170 (2026-09-15). Data as JSON: /api/errors/864711382d73945e. Report an issue: GitHub.

Appendix: source

Thrown at Harmony/Internal/MethodCopier.cs:29

{
	internal class MethodCopier
	{
		readonly MethodBodyReader reader;
		readonly List<MethodInfo> transpilers = [];

		internal MethodCopier(MethodBase fromMethod, ILGenerator toILGenerator, LocalBuilder[] existingVariables = null)
		{
			if (fromMethod is null)
				throw new ArgumentNullException(nameof(fromMethod));
			reader = new MethodBodyReader(fromMethod, toILGenerator);
			reader.DeclareVariables(existingVariables);
			reader.GenerateInstructions();
		}

		internal MethodCopier(MethodCreatorConfig config)
		{
			if (config.MethodBase is null)
				throw new ArgumentNullException("config.methodbase");
			reader = new MethodBodyReader(config.MethodBase, config.il);
			reader.DeclareVariables(config.originalVariables);
			reader.GenerateInstructions();
			reader.SetDebugging(config.debug);
		}

		internal void AddTranspiler(MethodInfo transpiler) => transpilers.Add(transpiler);

		internal List<CodeInstruction> Finalize(bool stripLastReturn, out bool hasReturnCode, out bool methodEndsInDeadCode, List<Label> endLabels)
			=> reader.FinalizeILCodes(transpilers, stripLastReturn, out hasReturnCode, out methodEndsInDeadCode, endLabels);

		internal static List<CodeInstruction> GetInstructions(ILGenerator generator, MethodBase method, int maxTranspilers)
		{
			if (generator is null)
				throw new ArgumentNullException(nameof(generator));
			if (method is null)
				throw new ArgumentNullException(nameof(method));

View on GitHub (pinned to e7872dc170)