dotnet/wpf · error · ArgumentException

InvalidValueOfType

Error message

InvalidValueOfType

What it means

For KnownIds.StylusTip, once the value is a StylusTip (or boxed int cast to StylusTip), ExtendedPropertySerializer checks StylusTipHelper.IsDefined. If the value is not a defined StylusTip enum member, an ArgumentException with SR.InvalidValueOfType is thrown.

Solutions

  1. Ensure the numeric value maps to a defined StylusTip member before assigning (use Enum.IsDefined)
  2. Sanitize deserialized data to a default (StylusTip.Ellipse) when undefined
  3. Catch ArgumentException and skip/repair the offending extended property

Example fix

// before
attrs[KnownIds.StylusTip] = (StylusTip)rawInt;
// after
var tip = Enum.IsDefined(typeof(StylusTip), rawInt) ? (StylusTip)rawInt : StylusTip.Ellipse;
attrs[KnownIds.StylusTip] = tip;
Defensive patterns

Strategy: validation

Validate before calling

int raw = ...;
if (!Enum.IsDefined(typeof(StylusTip), raw)) raw = (int)StylusTip.Ellipse;

Type guard

static bool IsDefinedStylusTip(StylusTip t) => t is StylusTip.Ellipse or StylusTip.Rectangle;

Try / catch

try { serializer.Serialize(...); }
catch (ArgumentException ex) when (ex.Message.Contains("StylusTip")) { /* sanitize and retry or skip */ }

Prevention

When it happens

Trigger: Storing an out-of-range or undefined numeric value as StylusTip (e.g. (StylusTip)99 or an int outside the defined range) in the extended properties and serializing/validating.

Common situations: Casting arbitrary ints from external files/user input to StylusTip; corrupt or hand-edited ink data; enum definitions changed between framework versions.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

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

                // ignore validation of flags
                if (value.GetType() != typeof(DrawingFlags))
                {
                    throw new ArgumentException(SR.Format(SR.InvalidValueType, typeof(DrawingFlags)), nameof(value));
                }
            }
            else if (id == KnownIds.StylusTip)
            {
                Type valueType = value.GetType();
                bool fStylusTipType = ( valueType == typeof(StylusTip) );
                bool fIntType = ( valueType == typeof(int) );

                if ( !fStylusTipType && !fIntType )
                {
                    throw new ArgumentException(SR.Format(SR.InvalidValueType1, typeof(StylusTip), typeof(int)), nameof(value));
                }
                else if ( !StylusTipHelper.IsDefined((StylusTip)value) )
                {
                    throw new ArgumentException(SR.Format(SR.InvalidValueOfType, value, typeof(StylusTip)), nameof(value));
                }
            }
            else if (id == KnownIds.StylusTipTransform)
            {
                //
                // StylusTipTransform gets serialized as a String, but at runtime is a Matrix
                //
                Type t = value.GetType();
                if ( t != typeof(String) && t != typeof(Matrix) )
                {
                    throw new ArgumentException(SR.Format(SR.InvalidValueType1, typeof(String), typeof(Matrix)), nameof(value));
                }
                else if ( t == typeof(Matrix) )
                {
                    Matrix matrix = (Matrix)value;
                    if ( !matrix.HasInverse )
                    {
                        throw new ArgumentException(SR.MatrixNotInvertible, nameof(value));

View on GitHub (pinned to 81131a70a4)