dotnet/wpf · error · ArgumentNullException

nameof(declaringType)

Error message

nameof(declaringType)

What it means

XamlMember's constructor throws ArgumentNullException when the 'declaringType' argument is null. Every XamlMember must be associated with a XamlType that owns the member; without it the member cannot be resolved in a schema context.

Solutions

  1. Ensure a valid non-null XamlType is created or looked up before constructing the XamlMember.
  2. Check why GetXamlType returned null (unregistered assembly/namespace in the XamlSchemaContext) and register the type.
  3. Wrap construction in a null check or use ArgumentNullException.ThrowIfNull on your own argument before forwarding.

Example fix

// before
var member = new XamlMember("MyProp", schemaContext.GetXamlType(typeof(MyType)), false);
// after
var declaringType = schemaContext.GetXamlType(typeof(MyType)) ?? throw new InvalidOperationException("Type not registered in schema context");
var member = new XamlMember("MyProp", declaringType, false);
Defensive patterns

Strategy: validation

Validate before calling

if (declaringType is null) throw new InvalidOperationException("declaringType must be resolved before creating a XamlMember");

Type guard

bool IsValid(XamlType t) => t is not null;

Prevention

When it happens

Trigger: Calling new XamlMember(string name, XamlType declaringType, bool isAttachable) with a null XamlType.

Common situations: Building custom XAML type systems or invokers where the declaring type is looked up dynamically (e.g. schemaContext.GetXamlType returned null) and the null result is passed straight into the constructor.

Related errors


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

Appendix: source

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

        private string _name;
        private XamlType _declaringType;
        private readonly MemberType _memberType;

        // Idempotent
        private ThreeValuedBool _isNameValid;

        // Thread safety: if setting outside ctor, do an interlocked compare against null
        private MemberReflector _reflector;

        /// <summary>
        /// Lazy init: NullableReference.IsSet is null when not initialized
        /// </summary>
        private NullableReference<MemberInfo> _underlyingMember;

        public XamlMember(string name, XamlType declaringType, bool isAttachable)
        {
            _name = name ?? throw new ArgumentNullException(nameof(name));
            _declaringType = declaringType ?? throw new ArgumentNullException(nameof(declaringType));
            _memberType = isAttachable ? MemberType.Attachable : MemberType.Instance;
        }

        // Known property
        public XamlMember(PropertyInfo propertyInfo, XamlSchemaContext schemaContext)
            :this(propertyInfo, schemaContext, null)
        {
        }

        public XamlMember(PropertyInfo propertyInfo, XamlSchemaContext schemaContext, XamlMemberInvoker invoker)
            : this(propertyInfo, schemaContext, invoker, new MemberReflector(isEvent: false))
        {
        }

        internal XamlMember(PropertyInfo propertyInfo, XamlSchemaContext schemaContext, XamlMemberInvoker invoker, MemberReflector reflector)
        {
            ArgumentNullException.ThrowIfNull(propertyInfo);
            ArgumentNullException.ThrowIfNull(schemaContext);

View on GitHub (pinned to 81131a70a4)