dotnet/wpf · error · InvalidOperationException

SR.MustNotCallSetter

Error message

SR.MustNotCallSetter

What it means

XmlNamespace.TargetNamespaceCore always returns XmlDirectives.Uri and its setter throws InvalidOperationException(SR.MustNotCallSetter). The XML xaml namespace is fixed to the standard XML directives URI, so retargeting it is an invalid operation by design.

Solutions

  1. Do not set TargetNamespace on XmlNamespace; it is permanently the XML directives URI.
  2. Create a different XamlNamespace instance if you need a namespace with another target URI.
  3. Skip XmlNamespace instances in code that mutates TargetNamespace (type-check before writing).

Example fix

// before
xmlNamespace.TargetNamespace = "http://my/ns";
// after
var ns = new XamlNamespace("http://my/ns"); // use a mutable namespace type instead
Defensive patterns

Strategy: type-guard

Validate before calling

if (ns is XmlNamespace) throw new NotSupportedException("XmlNamespace.TargetNamespace is fixed to the XML directives URI.");

Type guard

bool HasMutableTargetNamespace(XamlNamespace ns) => ns is not XmlNamespace;

Try / catch

try { ns.TargetNamespace = value; } catch (InvalidOperationException) { /* create a new namespace instance instead */ }

Prevention

When it happens

Trigger: Assigning to the TargetNamespace property (backed by TargetNamespaceCore) of an XmlNamespace instance, e.g. schema code that assumes every XamlNamespace has a writable target namespace.

Common situations: Generic namespace-manipulation utilities that rename namespaces; code copied from XaslNamespace handling where TargetNamespace is constructor-supplied and may be reassigned.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XmlNamespace.cs:94

            {
                directive = new UnknownProperty(name,
                                                declaringType: null, /* xml directives don't have a declaring type. */
                                                XmlDirectives.Uri);
            }
            return directive;
        }

        public override IEnumerable<XamlProperty> GetAllDirectiveProperties()
        {
            throw new NotImplementedException();
        }

        // --------------- protected ----------------------

        protected override string TargetNamespaceCore
        {
            get { return XmlDirectives.Uri; }
            set { throw new InvalidOperationException(SR.MustNotCallSetter); }
        }

        // -------------------------------------

        private XamlDirectiveCollection LoadDirectives()
        {
            XamlDirectiveCollection directives = null;

            switch (TargetNamespace)
            {
            case XmlDirectives.Uri:  // Uri == "http://www.w3.org/XML/1998/namespace"
                directives = new XamlDirectiveCollection(SchemaContext, XmlDirectives.DirectiveInfoTable);
                break;

            default:
                directives = new XamlDirectiveCollection(SchemaContext, null);
                break;
            }

View on GitHub (pinned to 81131a70a4)