pardeike/Harmony · error · ArgumentException

argumentVariations contains more elements than argumentTypes

Error message

argumentVariations contains more elements than argumentTypes

What it means

HarmonyPatch attributes accept argumentVariations (ArgumentType markers like Ref/Out/Normal) that must align positionally with argumentTypes. Harmony throws ArgumentException when there are more variation entries than parameter types, since each variation needs a matching type.

Solutions

  1. Make argumentVariations.Length <= argumentTypes.Length, pairing each variation with its type
  2. If a parameter has no special variation, omit its entry instead of adding a placeholder that extends the array
  3. If argumentTypes is empty/omitted, pass the full type list when using variations

Example fix

// before
[HarmonyPatch(typeof(Foo), nameof(Foo.Bar), argumentTypes: new[] { typeof(int) }, argumentVariations: new[] { ArgumentType.Normal, ArgumentType.Ref })]
// after
[HarmonyPatch(typeof(Foo), nameof(Foo.Bar), argumentTypes: new[] { typeof(int), typeof(string) }, argumentVariations: new[] { ArgumentType.Normal, ArgumentType.Ref })]
Defensive patterns

Strategy: validation

Validate before calling

if (argumentVariations is { Length: > 0 } && (argumentTypes is null || argumentVariations.Length > argumentTypes.Length))
    throw new InvalidOperationException("argumentVariations must not exceed argumentTypes");

Prevention

When it happens

Trigger: [HarmonyPatch(..., argumentTypes: new[]{typeof(int)}, argumentVariations: new[]{ArgumentType.Normal, ArgumentType.Ref})] — the variations array is longer than the types array. Also happens when argumentTypes is left to be inferred but variations are supplied explicitly.

Common situations: Adding a Ref/Out marker for a parameter you added to the patch signature but forgot to add to argumentTypes; copy-pasting attribute templates where the two arrays drifted out of sync.

Understand the failure class

Background: "Must be a positive integer", "Invalid value", "Unsupported": the invalid-argument-value error family, when a library rejects the value you pass — this error's family across 35 libraries.

Related errors


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

Appendix: source

Thrown at Harmony/Public/Attributes.cs:400

		/// <param name="methodType">The <see cref="MethodType"/></param>
		///
		public HarmonyPatch(string typeName, string methodName, MethodType methodType = MethodType.Normal)
		{
			info.declaringType = AccessTools.TypeByName(typeName);
			info.methodName = methodName;
			info.methodType = methodType;
		}

		void ParseSpecialArguments(Type[] argumentTypes, ArgumentType[] argumentVariations)
		{
			if (argumentVariations is null || argumentVariations.Length == 0)
			{
				info.argumentTypes = argumentTypes;
				return;
			}

			if (argumentTypes.Length < argumentVariations.Length)
				throw new ArgumentException("argumentVariations contains more elements than argumentTypes", nameof(argumentVariations));

			var types = new List<Type>();
			for (var i = 0; i < argumentTypes.Length; i++)
			{
				var type = argumentTypes[i];
				switch (argumentVariations[i])
				{
					case ArgumentType.Normal:
						break;
					case ArgumentType.Ref:
					case ArgumentType.Out:
						type = type.MakeByRefType();
						break;
					case ArgumentType.Pointer:
						type = type.MakePointerType();
						break;
				}
				types.Add(type);

View on GitHub (pinned to e7872dc170)