MonoGame/MonoGame · error · NotSupportedException

It seems that you are using PublishAot and trying to load as

Error message

It seems that you are using PublishAot and trying to load assets with a reflection-based serializer (which is not natively supported). To work around this error, call ContentTypeReaderManager.AddTypeCreator() in your Game constructor with the following type: {originalReaderTypeString}

What it means

When resolving a reader type string via Type.GetType(readerTypeString), older NativeAOT runtimes throw NotSupportedException because reflection-based type loading is unsupported under AOT. MonoGame catches it and re-throws a descriptive NotSupportedException instructing you to call ContentTypeReaderManager.AddTypeCreator for the named type string.

Source

Thrown at MonoGame.Framework/Content/ContentTypeReaderManager.cs:183

                    {
                        // Need to resolve namespace differences
                        string readerTypeString = originalReaderTypeString;

                        readerTypeString = PrepareType(readerTypeString);

                        Type l_readerType = null;
                        try
                        {
                            // This might fail in AOT context and we need to properly warn the user on what to do if it happens
#pragma warning disable IL2057
                            l_readerType = Type.GetType(readerTypeString);
#pragma warning restore IL2057
                        }
                        catch (NotSupportedException e)
                        {
                            // This will not trigger on recent NativeAOT versions, it will crash later on GetDefaultConstructor() with a native access violation
                            // but we keep this catch block for backward compatibility with older NativeAOT
                            throw new NotSupportedException("It seems that you are using PublishAot and trying to load assets with a reflection-based serializer (which is not natively supported). To work around this error, call ContentTypeReaderManager.AddTypeCreator() in your Game constructor with the following type: " + originalReaderTypeString);
                        }

                        if (l_readerType != null)
                        {
                            ContentTypeReader typeReader;
                            if (!_contentReadersCache.TryGetValue(l_readerType, out typeReader))
                            {
                                try
                                {
                                    typeReader = l_readerType.GetDefaultConstructor().Invoke(null) as ContentTypeReader;
                                }
                                catch (TargetInvocationException ex)
                                {
                                    // If you are getting here, the Mono runtime is most likely not able to JIT the type.
                                    // In particular, MonoTouch needs help instantiating types that are only defined in strings in Xnb files.
                                    throw new InvalidOperationException(
                                        "Failed to get default constructor for ContentTypeReader. To work around, add a creation function to ContentTypeReaderManager.AddTypeCreator() " +
                                        "with the following failed type string: " + originalReaderTypeString, ex);

View on GitHub (pinned to 1d71bbd0ff)

Solutions

  1. Call ContentTypeReaderManager.AddTypeCreator(typeof(TheReader).FullName, () => new TheReader()) in your Game constructor for the type named in the message.
  2. Keep reflection-based serialization off under AOT by pre-registering every reader you load.
  3. Note that many common readers are already auto-registered in the static constructor; only non-standard ones need manual registration.

Example fix

// before: Content.Load throws 266 under PublishAot
var tex = Content.Load<Texture2D>("sprite");

// after: register the missing reader type in the Game constructor
ContentTypeReaderManager.AddTypeCreator(
    "Microsoft.Xna.Framework.Content.Texture2DReader",
    () => new Texture2DReader());
Defensive patterns

Strategy: validation

Validate before calling

// Pre-register every content reader you intend to load under AOT.
ContentTypeReaderManager.AddTypeCreator(typeof(MyReader).FullName, () => new MyReader());

Try / catch

try { var asset = Content.Load<T>("x"); }
catch (NotSupportedException ex) when (ex.Message.Contains("PublishAot"))
{ var typeName = ExtractTypeName(ex); ContentTypeReaderManager.AddTypeCreator(typeName, () => /*new reader*/); }

Prevention

When it happens

Trigger: Publishing with PublishAot / NativeAOT and loading an XNB asset whose reader type is not pre-registered, so Type.GetType fails at runtime.

Common situations: Moving a project to PublishAot or enabling trimming without registering all required content readers; loading content whose reader type only appears as a string in the XNB.

Related errors


AI-assisted analysis of MonoGame/MonoGame@1d71bbd0ff (2026-08-13). Data as JSON: /api/errors/175e7972d4ba1a8a. Report an issue: GitHub.