dotnet/wpf · error · XamlSchemaException

SR.Format(SR.TooManyAttributesOnType, reflectedType.Name…

Error message

SR.Format(SR.TooManyAttributesOnType, reflectedType.Name, attrType.Name)

What it means

TypeReflector's public single-attribute lookup throws XamlSchemaException when more than one attribute of the requested type (attrType) is applied to the reflected type. Type-level schema attributes are expected to appear at most once; multiple instances make the value ambiguous.

Solutions

  1. Remove the duplicate attribute from the type named in the exception message.
  2. Enumerate all attribute instances with GetCustomAttributes instead of the single-attribute API when multiple are intentional.
  3. Ensure build/merge tooling is not duplicating attribute declarations.

Example fix

// before
[ContentProperty("Items")]
[ContentProperty("Children")]
class Panel { }

// after
[ContentProperty("Items")]
class Panel { }
Defensive patterns

Strategy: validation

Validate before calling

// assert at most one attrType per type before schema lookup
var count = type.GetCustomAttributes(attrType, inherit: true).Length;
if (count > 1) throw new InvalidOperationException($"{type.Name} has {count} {attrType.Name}; expected at most 1");

Type guard

bool TypeHasSingle<TAttr>(Type t) => t.GetCustomAttributes(typeof(TAttr), true).Length == 1;

Try / catch

try { var a = typeReflector.GetCustomAttribute(attrType); }
catch (XamlSchemaException ex) { log(ex.Message); /* deduplicate the attribute on the type */ }

Prevention

When it happens

Trigger: Calling the public TypeReflector method that fetches one attribute of attrType on a type carrying two or more instances of that attribute, e.g. two [XmlnsDefinition] or duplicate custom type-level metadata attributes.

Common situations: Duplicate attribute lines left by copy-paste on a class; an attribute marked AllowMultiple=true that the schema layer reads as single; assembly post-processing (weaving, merging) re-applying attributes.

Understand the failure class

Background: Schema validation failed / invalid input schema: payload rejected because its shape doesn't match the expected schema — this error's family across 28 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/TypeReflector.cs:1098

        // Used by Reflector for attribute lookups
        protected override MemberInfo Member
        {
            get { return UnderlyingType; }
        }

        private static object GetCustomAttribute(Type attrType, Type reflectedType)
        {
            object[] objs = reflectedType.GetCustomAttributes(attrType, true);
            if (objs.Length == 0)
            {
                return null;
            }

            if (objs.Length > 1)
            {
                string message = SR.Format(SR.TooManyAttributesOnType,
                                                    reflectedType.Name, attrType.Name);
                throw new XamlSchemaException(message);
            }

            return objs[0];
        }

        private static TypeVisibility GetVisibility(Type type)
        {
            bool nestedTypeIsInternal = false;
            // If the type is nested, we need to check its entire declaring hierarchy
            while (type.IsNested)
            {
                if (type.IsNestedAssembly || type.IsNestedFamORAssem)
                {
                    nestedTypeIsInternal = true;
                }
                else if (!type.IsNestedPublic)
                {
                    // Not public or internal

View on GitHub (pinned to 81131a70a4)