dotnet/wpf · error · InvalidOperationException

SR.SetTargetTypeOnNonAttachableMember

Error message

SR.SetTargetTypeOnNonAttachableMember

What it means

XaslMember.TargetTypeCore setter throws InvalidOperationException because TargetType on a non-attachable member must never be mutated. The library treats the target type of ordinary members as derived state that callers cannot assign; only attachable members have a settable target type.

Solutions

  1. Do not assign TargetType on non-attachable members; construct the member with the correct target type instead
  2. Implement a custom XamlMember subclass overriding TargetTypeCore only for attachable members
  3. Use the protected constructor of XamlMember that accepts an attachable property info so TargetType is populated correctly

Example fix

// before
member.TargetType = targetType; // throws

// after
var attachable = new XamlMember(attachablePropertyInfo, schemaContext); // TargetType derived from the attached property
Defensive patterns

Strategy: validation

Validate before calling

if (member == null) throw new ArgumentNullException(nameof(member));
if (member.IsAttachable)
    member.TargetType = newType; // legal only for attachable members
else
    throw new InvalidOperationException("TargetType is read-only for non-attachable members.");

Type guard

bool CanSetTargetType(XamlMember m) => m is XamlMember { IsAttachable: true };

Try / catch

try
{
    member.TargetType = newType;
}
catch (InvalidOperationException ex)
{
    logger.Warn(ex, "Cannot set TargetType on non-attachable member {0}", member.Name);
}

Prevention

When it happens

Trigger: Assigning to XamlMember.TargetType (or the TargetTypeCore override) on a member that is not attachable — e.g. member.TargetType = someType on a property fetched via XamlType.GetMember.

Common situations: Framework/tooling code that tries to re-target a regular XAML property during schema manipulation or custom schema provider implementations that call the base setter.

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/d9ba27eb1b17a3da. Report an issue: GitHub.

Appendix: source

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

        {
            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; }
            set { _isAmbient = value; }

View on GitHub (pinned to 81131a70a4)