dotnet/wpf · error · ArgumentException

SR.IncorrectSetterParamNum

Error message

SR.IncorrectSetterParamNum

What it means

ValidateSetter rejects setter MethodInfo objects that do not have exactly two parameters, throwing ArgumentException with SR.IncorrectSetterParamNum. An attachable setter must be static void SetX(object target, TValue value).

Solutions

  1. Change the setter to have exactly two parameters: (object target, TValue value).
  2. Pass a correct MethodInfo instead.
  3. Pass null for the setter if only a getter is intended.

Example fix

// before
public static void SetMyProp(string value) { ... }
// after
public static void SetMyProp(object target, string value) { ... }
Defensive patterns

Strategy: validation

Validate before calling

if (setter is not null && setter.GetParameters().Length != 2) throw new ArgumentException("Setter must take exactly 2 parameters");

Type guard

bool IsValidSetter(MethodInfo m) => m is null || m.GetParameters().Length == 2;

Prevention

When it happens

Trigger: Constructing an attachable-property XamlMember or attachable-event XamlMember with a setter/adder MethodInfo whose parameter count is not 2.

Common situations: Attached-event Add handlers missing the sender parameter, or setters written as instance methods with one value parameter.

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 dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/ef9342357f2fbbbc. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlMember.cs:857

        private static void ValidateGetter(MethodInfo method, string argumentName)
        {
            if (method is null)
            {
                return;
            }

            if ((method.GetParameters().Length != 1) || (method.ReturnType == typeof(void)))
            {
                throw new ArgumentException(SR.IncorrectGetterParamNum, argumentName);
            }
        }

        private static void ValidateSetter(MethodInfo method, string argumentName)
        {
            if ((method is not null) && (method.GetParameters().Length != 2))
            {
                throw new ArgumentException(SR.IncorrectSetterParamNum, argumentName);
            }
        }

        // This property needs to be checked before any attribute lookups on _reflector. It's not
        // only informational, it also ensures that the right state is initialized.
        private bool AreAttributesAvailable
        {
            get
            {
                EnsureReflector();

                // Make sure that AttributeProvider is initialized
                // Note: Don't short-circuit the AttributeProvider lookup, even if UnderlyingMember
                // is non-null; a derived class can use AttributeProvider to override attribute lookup

                // Volatile read/write of CustomAttributeProvider to make sure that threads that see
                // CustomAttributeProviderIsSet == true also see the write to _reflector.UnderlyingMember
                if (!_reflector.CustomAttributeProviderIsSetVolatile)

View on GitHub (pinned to 81131a70a4)