dotnet/wpf · error

SR.UnserializableKeyValue

Error message

SR.UnserializableKeyValue

What it means

MarkupWriter.WriteItem throws this plain InvalidOperationException (SR.UnserializableKeyValue) when a MarkupObject yields a property flagged IsConstructorArgument. The XAML writer cannot emit constructor arguments (the reader-side <x:ArgumentN> format was never implemented), so any object requiring constructor-argument properties cannot be serialized. The surrounding comment marks it as an intentional stop-gap exception.

Solutions

  1. Avoid serializing that type with MarkupWriter/XamlWriter; serialize its properties individually or via a convertible surrogate type.
  2. Add a TypeConverter with ConvertToString/ConvertFrom support so the value is emitted as a string instead of constructor-argument properties.
  3. Provide a designer markup override (AttributeTable / ValueSerializer) that eliminates the constructor-argument property.
Defensive patterns

Strategy: try-catch

Validate before calling

// any property with IsConstructorArgument => unsupported
bool HasConstructorArgumentProps(object o) =>
    MarkupWriter.GetMarkupObjectFor(o).Properties.Any(p => p.IsConstructorArgument);

Try / catch

try { XamlWriter.Save(obj, stream); }
catch (InvalidOperationException ex)
{
    // use DataContractSerializer or TypeConverter-based output instead
}

Prevention

When it happens

Trigger: Serializing (XamlWriter.Save / MarkupWriter.WriteItem) a type whose markup provider exposes IsConstructorArgument properties - e.g. types with required constructor parameters mapped by WPF markup metadata as constructor arguments.

Common situations: Saving objects of types whose value-type marshaling is defined through constructor arguments (e.g. some structs via their markup implementation); this indicates the type cannot be round-tripped by this writer.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs:501

            MarkupProperty contentProperty = null;

            bool first = true;
            int argumentCount = 0;
            List<int> argumentCompositeIndexes = null;
            bool noOtherPropertiesAllowed = false;
            List<MarkupProperty> composites = null;
            Dictionary<string, string> writtenAttributes = new Dictionary<string, string>();
            PartiallyOrderedList<string, MarkupProperty> deferredProperties = null;
            Formatting previousFormatting = (_xmlTextWriter != null) ? _xmlTextWriter.Formatting : Formatting.None;

            foreach (MarkupProperty property in item.GetProperties(false /*mapToConstructorArgs*/))
            {
                if (property.IsConstructorArgument)
                {
                    // When the reader supports <x:Argument1>...</x:Argument1> format, do the following:
                    //   _writer.WriteStartElement(string.Format(CultureInfo.InvariantCulture, "Argument{0}", argumentCompositeIndexes[argumentCompositeIndex++]), NamespaceCache.XamlNamespace);
                    // The writer generates an exception for now:
                    throw new InvalidOperationException(SR.UnserializableKeyValue);
                }
                Debug.Assert(!noOtherPropertiesAllowed || property.IsKey,
                    "Problem with MarkupObject implemenation: Items returning a ValueAsString can have no other properties");

                if (IsContentProperty(property, cpa, ref contentProperty))
                {
                    first = false;
                    continue;
                }
                if (IsDeferredProperty(property, writtenAttributes, ref deferredProperties))
                {
                    continue;
                }

                if (!property.IsComposite)
                {
                    if (property.IsAttached || property.PropertyDescriptor == null)
                    {

View on GitHub (pinned to 81131a70a4)