dotnet/wpf · error · XamlSchemaException

SR.Format(SR.UnexpectedConstructorArg…

Error message

SR.Format(SR.UnexpectedConstructorArg, cad.Constructor.DeclaringType, Member, expectedCount, expectedType)

What it means

Reflector.ThrowInvalidMetadata throws XamlSchemaException when a custom attribute's constructor arguments do not match what the XAML metadata reader expects. The message reports the attribute's declaring type, the member being reflected, the expected argument count, and the expected argument type. It guards against malformed or hand-crafted attribute metadata.

Solutions

  1. Fix the attribute usage at the member named in the message so its constructor arguments match the expected count/type.
  2. Rebuild the assembly so attribute metadata is emitted by the normal compiler instead of post-processing tools.
  3. Align the referenced System.Xaml/WPF version with the one the attributes were compiled against.

Example fix

// before
[XmlnsDefinition("http://example/ns")] // missing second (assembly) argument
public class Foo { }

// after
[XmlnsDefinition("http://example/ns", "MyAssembly")]
public class Foo { }
Defensive patterns

Strategy: validation

Validate before calling

// verify attribute constructor args before extraction
var cad = attributeData;
if (cad.ConstructorArguments.Count != expectedCount ||
    !expectedType.IsInstanceOfType(cad.ConstructorArguments[0].Value))
    throw new InvalidOperationException($"{cad.Constructor.DeclaringType}: bad ctor args for {member.Name}");

Type guard

bool HasValidCtorArgs(CustomAttributeData cad, int count, Type t) => cad.ConstructorArguments.Count == count && cad.ConstructorArguments.All(a => t.IsInstanceOfType(a.Value));

Try / catch

try { types = Reflector.ExtractTypes(member); }
catch (XamlSchemaException ex) { log($"Bad attribute metadata: {ex.Message}"); /* rebuild assembly or fix attribute */ }

Prevention

When it happens

Trigger: Called from ExtractType/ExtractTypes/Extract when parsing a CustomAttributeData whose ConstructorInvoke arguments have the wrong count or wrong type for the attribute kind being extracted (e.g. an attribute expecting one Type argument receives an int, or receives two arguments instead of one).

Common situations: Building attributes via reflection emit or IL weavers with wrong constructor signatures; referencing an attribute overload that doesn't match the XAML metadata contract; assembly compiled against a different System.Xaml version where the attribute constructor changed.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/Reflector.cs:544

            return null;
        }

        private void GetAttributes(Type attributeType, IList<CustomAttributeData> cads)
        {
            EnsureAttributeData();
            for (int i = 0; i < _attributeData.Count; i++)
            {
                if (TypesAreEqual(_attributeData[i].Constructor.DeclaringType, attributeType))
                {
                    cads.Add(_attributeData[i]);
                }
            }
        }

        protected void ThrowInvalidMetadata(CustomAttributeData cad, int expectedCount, Type expectedType)
        {
            throw new XamlSchemaException(SR.Format(SR.UnexpectedConstructorArg,
                cad.Constructor.DeclaringType, Member, expectedCount, expectedType));
        }
    }
}

View on GitHub (pinned to 81131a70a4)