{"record":{"id":"9f479f0a28b324bd","repo":"dotnet/wpf","slug":"sr-notsupportedondirective","errorCode":null,"errorMessage":"SR.NotSupportedOnDirective","messagePattern":"SR\\.NotSupportedOnDirective","errorType":"exception","errorClass":"NotSupportedException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlMemberInvoker.cs","lineNumber":167,"sourceCode":"\n        private bool IsUnknown\n        {\n            get { return _member is null || _member.UnderlyingMember is null; }\n        }\n\n        private void ThrowIfUnknown()\n        {\n            if (IsUnknown)\n            {\n                throw new NotSupportedException(SR.NotSupportedOnUnknownMember);\n            }\n        }\n\n        private class DirectiveMemberInvoker : XamlMemberInvoker\n        {\n            public override object GetValue(object instance)\n            {\n                throw new NotSupportedException(SR.NotSupportedOnDirective);\n            }\n\n            public override void SetValue(object instance, object value)\n            {\n                throw new NotSupportedException(SR.NotSupportedOnDirective);\n            }\n        }\n    }\n\n    public enum ShouldSerializeResult\n    {\n        Default,\n        True,\n        False\n    }\n}\n","sourceCodeStart":149,"sourceCodeEnd":184,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Schema/XamlMemberInvoker.cs#L149-L184","documentation":"DirectiveMemberInvoker (used for XAML directives like x:Name, x:Key) overrides GetValue to always throw NotSupportedException with SR.NotSupportedOnDirective. Directives are language constructs, not CLR properties, so there is no backing value to read through the invoker.","triggerScenarios":"Calling GetValue on the invoker of a XamlDirective (any member obtained from XamlLanguage.Name, XamlLanguage.Key, XamlLanguage.Class, etc.).","commonSituations":"Generic member-enumeration code that iterates type.GetAllMembers() (which includes directives) and tries to read each value; serializers dumping object graphs that encounter directive members.","solutions":["Skip directives when enumerating: filter members where `member is XamlDirective` before calling GetValue.","Handle directive semantics explicitly (e.g. read x:Name from the actual NameScope/name mechanism) instead of through the invoker.","Catch NotSupportedException around GetValue if the member set is not known in advance."],"exampleFix":"// before\nforeach (var m in type.GetAllMembers()) Read(m); // throws on x:Name etc.\n// after\nforeach (var m in type.GetAllMembers())\n{\n    if (m is XamlDirective) continue;\n    Read(m);\n}","handlingStrategy":"type-guard","validationCode":"if (member is XamlDirective) return; // no value to read","typeGuard":"static bool IsDirective(XamlMember m) => m is XamlDirective;","tryCatchPattern":"try { v = invoker.GetValue(instance); }\ncatch (NotSupportedException) { /* directive: no CLR value */ }","preventionTips":["Filter out XamlDirective instances when enumerating GetAllMembers()","Implement explicit handling for directives you care about (x:Name, x:Key)","Remember directives have no CLR backing property"],"tags":["wpf","system-xaml","not-supported-exception","xaml-directive"],"backgroundTag":"unsupported-operation","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}