dotnet/wpf · error · XpsSerializationException

Cannot instantiate a serializer of the given type.

Error message

Cannot instantiate a serializer of the given type.

What it means

SerializersCacheManager instantiates ReachSerializer types via Activator.CreateInstance and casts the result to ReachSerializer. If the created instance is not a ReachSerializer (cast returns null), it throws XpsSerializationException 'Cannot instantiate a serializer of the given type'. The serializer type registered/mapped for the CLR type does not derive from ReachSerializer.

Solutions

  1. Ensure the serializer type registered for your object type derives from ReachSerializer and has a constructor accepting the serialization manager
  2. Check which type you are passing to SaveAsXaml and confirm it has a built-in serializer
  3. Register/fix the custom serializer mapping

Example fix

// before
// registered serializer does not inherit ReachSerializer
public class MyCustomSerializer { ... }
// after
public class MyCustomSerializer : ReachSerializer { public MyCustomSerializer(SerializationManager m) : base(m) {} }
Defensive patterns

Strategy: type-guard

Validate before calling

if (!typeof(ReachSerializer).IsAssignableFrom(serializerType))
    throw new InvalidOperationException($"{serializerType} must derive from ReachSerializer");

Type guard

bool IsValidSerializerType(Type t) =>
    t != null && typeof(ReachSerializer).IsAssignableFrom(t) &&
    t.GetConstructor(new[] { typeof(SerializationManager) }) != null;

Try / catch

try
{
    manager.SaveAsXaml(obj);
}
catch (XpsSerializationException ex) when (ex.Message.Contains("instantiate"))
{
    // fix serializer registration for obj's type
}

Prevention

When it happens

Trigger: Serializing an object whose mapped serializer type is not a ReachSerializer subclass or whose constructor does not accept the serialization manager argument, causing the cast to ReachSerializer to yield null.

Common situations: Custom serializer registration mistakes (registering a type that does not inherit ReachSerializer), or framework serializer-type mapping table inconsistencies for unusual object types.

Understand the failure class

Background: "Invalid value" and "allowed values are" config errors: what your library rejected and how to fix it — this error's family across 41 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/ReachFramework/Serialization/manager/MetroSerializationManager.cs:1076

            }

            return canSerializeValue;
        }

        private 
        ReachSerializer
        CreateReachSerializer(Type serializerType)
        {
            MS.Internal.Invariant.Assert(serializerType != null);
            MS.Internal.Invariant.Assert(serializerType.IsSubclassOf(typeof(ReachSerializer)));

            object[] args = new object[] { _serializationManager };

            ReachSerializer result = Activator.CreateInstance(serializerType, args) as ReachSerializer;

            if (result == null)
            {
                throw new XpsSerializationException(SR.ReachSerialization_UnableToInstantiateSerializer);
            }

            return result;
        }

        #endregion Private Methods

        #region Private Data Members

        private 
        PackageSerializationManager   _serializationManager;

        //
        // The following is being cached for future reference and to
        // optimize performance
        // 1. The Types and their clr properties
        // 2. The Types and their corresponding dependency properties. Since,
        //    not all types have dependency properties, that is why I am

View on GitHub (pinned to 81131a70a4)