dotnet/wpf · error · InvalidOperationException

SR.GetTargetTypeOnNonAttachableMember

Error message

SR.GetTargetTypeOnNonAttachableMember

What it means

XaslMember.TargetTypeCore throws InvalidOperationException when the getter is called on a XamlMember that is not an attachable member. TargetType is only meaningful for attachable members (e.g. `x:Static`-style attached properties), whose target type can differ from the declaring type. The base class deliberately rejects the call so consumers do not silently get a wrong type.

Solutions

  1. Check member.IsAttachable before reading TargetType and skip or handle non-attachable members separately
  2. For regular members, use the DeclaringType property instead of TargetType
  3. Wrap the read in a try/catch for InvalidOperationException only if IsAttachable cannot be checked

Example fix

// before
XamlType tt = member.TargetType;

// after
XamlType tt = member.IsAttachable ? member.TargetType : member.DeclaringType;
Defensive patterns

Strategy: type-guard

Validate before calling

if (member == null) throw new ArgumentNullException(nameof(member));
if (member.IsAttachable)
{
    var tt = member.TargetType; // safe
}
else
{
    var declaring = member.DeclaringType; // use instead
}

Type guard

bool IsAttachableMember(XamlMember m) => m != null && m.IsAttachable;

Try / catch

try
{
    var tt = member.TargetType;
}
catch (InvalidOperationException ex) when (ex.Message.Contains("attachable"))
{
    // fall back to member.DeclaringType
    var tt = member.DeclaringType;
}

Prevention

When it happens

Trigger: Accessing the TargetType property (or TargetTypeCore) of a XamlMember instance that represents a regular (non-attachable) property, event, or directive — e.g. calling member.TargetType after obtaining a member from a XamlType.GetMember call.

Common situations: Tooling or serializers that enumerate XAML members and unconditionally read TargetType for every member; code that assumes all members obtained from a schema context are attachable; reflection-style XAML inspection utilities.

Understand the failure class

Background: UnsupportedOperationException and "is not supported" errors: when a library deliberately refuses a call — this error's family across 30 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XaslMember.cs:217

        protected override bool IsReadOnlyCore
        {
            get { return _isReadOnly; }
            set { _isReadOnly = value; }
        }

        protected override bool IsStaticCore
        {
            get { return _isStatic; }
            set { _isStatic = value; }
        }

        // Leave these to derived types.
        //protected override bool IsAttachableCore { get; set; }
        //protected override bool IsEventCore { get; set; }
        //protected override bool IsDirectiveCore { get; set; }
        protected override XamlType TargetTypeCore
        {
            get { throw new InvalidOperationException(SR.GetTargetTypeOnNonAttachableMember); }
            set { throw new InvalidOperationException(SR.SetTargetTypeOnNonAttachableMember); }
        }

        protected override AllowedMemberLocation AllowedLocationCore
        {
            get { return _allowedLocation; }
            set { _allowedLocation = value; }
        }

        protected override XamlProperty DependsOnCore
        {
            get { return _dependsOn; }
            set { _dependsOn = value; }
        }

        protected override bool IsAmbientCore
        {
            get { return _isAmbient; }

View on GitHub (pinned to 81131a70a4)