dotnet/wpf · error · XamlSchemaException

Attached property setter and attached event adder methods…

Error message

Attached property setter and attached event adder methods must have two parameters.

What it means

System.Xaml requires attachable property setters and attached event adder methods to take exactly two parameters (the target object and the value/event-handler). When the resolved setter MethodInfo has only one parameter, the schema throws this XamlSchemaException while computing the setter's parameter type.

Solutions

  1. Change the setter/adder signature to (object target, Type value) — two parameters — e.g. SetFoo(DependencyObject o, MyType value).
  2. Add the missing target-object parameter to the Set<Name> or Add<Name>Handler method.
  3. Point the schema registration at the correct MethodInfo overload that has two parameters.

Example fix

// before
public static void SetGlow(double value) { ... }
// after
public static void SetGlow(DependencyObject target, double value) { ... }
Defensive patterns

Strategy: validation

Validate before calling

// verify attached setter/adder arity before registration
var p = setterMethodInfo.GetParameters();
if (p.Length != 2)
    throw new InvalidOperationException($"{setterMethodInfo.Name} must take (target, value): found {p.Length} params");

Type guard

bool IsValidAttachedSetter(MethodInfo m) =>
    m.IsStatic && m.GetParameters().Length == 2;

Try / catch

try { member = new ClrAttachedProperty(name, declaringType, getter, setter); }
catch (XamlSchemaException ex) { log($"Bad attached member signature: {ex.Message}"); }

Prevention

When it happens

Trigger: ClrAttachedProperty resolves ClrBindingSetterMethodInfo whose GetParameters() has Length <= 1, then indexing parameters[1] fails and SR.IncorrectSetterParamNum is thrown with the method name and expected count 2.

Common situations: Hand-written Set<Name>(value) methods missing the target-object first parameter; attached-event add handlers written with one argument instead of (object, handler); partial refactors that dropped the target parameter.

Understand the failure class

Background: "missing required argument" and "the following required arguments were not provided": what required-argument errors mean and how to fix them — this error's family across 20 libraries.

Related errors


AI-assisted analysis of dotnet/wpf@81131a70a4 (2026-09-14). Data as JSON: /api/errors/fc8a760817bac499. Report an issue: GitHub.

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/ClrAttachableProperty.cs:76

        private Type PrivateLookupSystemTypeOfProperty
        {
            get 
            {
                if (ClrBindingGetterMethodInfo != null)
                {
                    return ClrBindingGetterMethodInfo.ReturnType;
                }
                else
                {
                    ParameterInfo[] pis = ClrBindingSetterMethodInfo.GetParameters();
                    if (pis.Length > 1)
                    {
                        return ClrBindingSetterMethodInfo.GetParameters()[1].ParameterType;
                    }
                    else
                    {
                        throw new XamlSchemaException(SR.Format(SR.IncorrectSetterParamNum, ClrBindingSetterMethodInfo.Name, 2));
                    }
                }
            }
        }

        protected override object[] LookupCustomAttributes(Type attrType)
        {
            if (ClrBindingGetterMethodInfo != null)
            {
                return ClrBindingGetterMethodInfo.GetCustomAttributes(attrType, true);
            }
            return ClrBindingSetterMethodInfo.GetCustomAttributes(attrType, true);
        }

        protected override XamlType LookupTargetType()
        {
            MethodInfo mi = (ClrBindingGetterMethodInfo != null)
                ? ClrBindingGetterMethodInfo 

View on GitHub (pinned to 81131a70a4)