dotnet/wpf · error · ArgumentException

InvalidValueType

Error message

InvalidValueType

What it means

Thrown by ExtendedPropertySerializer.Validate when the known GUID (KnownIds.Color) is used but the stored value is not a System.Windows.Media.Color. Known GUIDs have fixed expected value types so the ISF encoder can serialize them; a wrong-typed value cannot be encoded correctly.

Solutions

  1. Store a System.Windows.Media.Color value for the Color known GUID (convert first if needed).
  2. If the source is a WinForms System.Drawing.Color, convert via Color.FromArgb(a,r,g,b) into Media.Color.
  3. If the value is a string/int, choose a custom GUID for it instead of the Color known id, or parse it into a Media.Color.

Example fix

// before
drawingAttributes.ExtendedProperties.Add(KnownIds.Color, "#FF0000");
// after
var color = (Color)ColorConverter.ConvertFromString("#FF0000");
drawingAttributes.ExtendedProperties.Add(KnownIds.Color, color);
Defensive patterns

Strategy: type-guard

Validate before calling

if (id == KnownIds.Color && value is not System.Windows.Media.Color)
    throw new ArgumentException("Color extended property requires System.Windows.Media.Color", nameof(value));

Type guard

bool IsValidColorValue(object v) => v is System.Windows.Media.Color;

Try / catch

try { props.Add(KnownIds.Color, value); }
catch (ArgumentException ex) when (ex.ParamName == "value") { props.Add(KnownIds.Color, ConvertToMediaColor(value)); }

Prevention

When it happens

Trigger: Assigning an extended property under the Color known GUID with a non-Color value (e.g. a string like "#FF0000", an int, or a System.Drawing.Color from WinForms).

Common situations: Interop confusion between System.Drawing.Color and System.Windows.Media.Color; storing color as hex string in extended properties then serializing to ISF.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationCore/MS/internal/Ink/InkSerializedFormat/CustomAttributeSerializer.cs:780

        #region Key/Value pair validation helpers
        /// <summary>
        /// Validates the data to be associated with a ExtendedProperty id
        /// </summary>
        /// <param name="id">ExtendedProperty identifier</param>
        /// <param name="value">data</param>
        /// <remarks>Ignores Ids that are not known (e.g. ExtendedProperties)</remarks>
        internal static void Validate(Guid id, object value)
        {
            if (id == Guid.Empty)
            {
                throw new ArgumentException(SR.InvalidGuid);
            }

            if (id == KnownIds.Color)
            {
                if (!(value is System.Windows.Media.Color))
                {
                    throw new ArgumentException(SR.Format(SR.InvalidValueType, typeof(System.Windows.Media.Color)), nameof(value));
                }
            }
                // int attributes
            else if (id == KnownIds.CurveFittingError)
            {
                if (!(value.GetType() == typeof(int)))
                {
                    throw new ArgumentException(SR.Format(SR.InvalidValueType, typeof(int)), nameof(value));
                }
            }
            else if (id == KnownIds.DrawingFlags)
            {
                // ignore validation of flags
                if (value.GetType() != typeof(DrawingFlags))
                {
                    throw new ArgumentException(SR.Format(SR.InvalidValueType, typeof(DrawingFlags)), nameof(value));
                }
            }

View on GitHub (pinned to 81131a70a4)