dotnet/wpf · error · NotSupportedException

Cannot get write-only property

Error message

Cannot get write-only property '{0}'.

What it means

DynamicMethodRuntime.GetValue is called to read a member's value, but the member's invoker has no UnderlyingGetter (the property is write-only). It throws NotSupportedException with CantGetWriteonlyProperty ('Cannot get a write-only property {0}'). The library throws this because there is no getter method to invoke for the property.

Solutions

  1. Add a public getter to the property, or make it read-write if serialization requires round-tripping
  2. Exclude the write-only property from XAML by making it internal/private or marking the type appropriately for the schema
  3. Use a different member (read-only wrapper or attached representation) to convey the value in XAML

Example fix

// before
public string Secret { set { _secret = value; } }
// after
public string Secret { get; set; }
Defensive patterns

Strategy: try-catch

Validate before calling

static void EnsureReadable(Type t, string prop) { var p = t.GetProperty(prop); if (p != null && p.GetMethod == null) throw new InvalidOperationException($"{t}.{prop} is write-only and cannot be XAML-read"); }

Type guard

static bool IsXamlReadable(System.Reflection.PropertyInfo p) => p?.GetMethod is { IsPublic: true };

Try / catch

try { Save(obj); } catch (NotSupportedException ex) when (ex.Message.Contains("write-only property")) { throw new InvalidOperationException($"Exclude write-only member: {ex.Message}", ex); }

Prevention

When it happens

Trigger: XAML load or save attempts to read (get) a member whose CLR property defines only a setter — e.g. XamlXmlWriter or XamlObjectWriter querying a write-only property, or a positional/mapped member whose getter is missing.

Common situations: Objects with write-only properties (e.g. password fields) included in XAML-serializable types; WPF dependency properties or wrappers exposing only setters; schema/type mapping pointing at a property without a getter.

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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/Runtime/DynamicMethodRuntime.cs:243

        protected override object InvokeFactoryMethod(Type type, string methodName, object[] args)
        {
            MethodInfo factory = GetFactoryMethod(type, methodName, args, BF_AllStaticMembers);
            FactoryDelegate factoryDelegate;
            if (!FactoryDelegates.TryGetValue(factory, out factoryDelegate))
            {
                factoryDelegate = CreateFactoryDelegate(factory);
                FactoryDelegates.Add(factory, factoryDelegate);
            }

            return factoryDelegate.Invoke(args);
        }

        protected override object GetValue(XamlMember member, object obj)
        {
            MethodInfo getter = member.Invoker.UnderlyingGetter;
            if (getter is null)
            {
                throw new NotSupportedException(SR.Format(SR.CantGetWriteonlyProperty, member));
            }

            PropertyGetDelegate getterDelegate;
            if (!PropertyGetDelegates.TryGetValue(getter, out getterDelegate))
            {
                getterDelegate = CreateGetDelegate(getter);
                PropertyGetDelegates.Add(getter, getterDelegate);
            }

            return getterDelegate.Invoke(obj);
        }

        protected override void SetValue(XamlMember member, object obj, object value)
        {
            MethodInfo setter = member.Invoker.UnderlyingSetter;
            if (setter is null)
            {
                throw new NotSupportedException(SR.Format(SR.CantSetReadonlyProperty, member));

View on GitHub (pinned to 81131a70a4)