dotnet/wpf · error · ArgumentNullException

nameof(name)

Error message

nameof(name)

What it means

The XamlMember(string name, XamlType declaringType, bool isAttachable) constructor requires a non-null member name; the name is fundamental to lookup, ToString, and comparison. Passing null for `name` throws ArgumentNullException(nameof(name)). (Note: the constructor also throws the same for a null declaringType.)

Solutions

  1. Ensure the name string is non-null before constructing; substitute a real member name or fail earlier with a clear message
  2. If the name may be missing, throw a descriptive validation error upstream instead of passing null into XamlMember
  3. Verify the reflection source (PropertyInfo/FieldInfo) is valid before reading its Name

Example fix

// before
var member = new XamlMember(config.Name, declaringType, false); // config.Name may be null
// after
if (string.IsNullOrEmpty(config.Name)) throw new InvalidOperationException("Member name missing in config");
var member = new XamlMember(config.Name, declaringType, false);
Defensive patterns

Strategy: validation

Validate before calling

if (string.IsNullOrEmpty(name)) throw new ArgumentException("Member name must be non-empty", nameof(name));
if (declaringType is null) throw new ArgumentNullException(nameof(declaringType));
var member = new XamlMember(name, declaringType, isAttachable);

Type guard

static bool IsValidMemberName(string name) => !string.IsNullOrEmpty(name);

Try / catch

try { member = new XamlMember(name, declaringType, isAttachable); }
catch (ArgumentNullException ex) when (ex.ParamName == "name") { /* resolve a real member name before constructing */ }

Prevention

When it happens

Trigger: Calling new XamlMember(null, declaringType, isAttachable) — e.g. deriving a member name from reflection where a PropertyInfo/FieldInfo name was null or the variable was uninitialized.

Common situations: Custom XamlSchemaContext implementations building members dynamically; metadata-driven code generation where member names come from config or strings parsed at runtime that may be null.

Related errors


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

Appendix: source

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

        // Initialized in constructor
        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);

View on GitHub (pinned to 81131a70a4)