dotnet/wpf · error · XamlObjectReaderException

SR.ObjectReaderTypeNotAllowed

Error message

SR.ObjectReaderTypeNotAllowed

What it means

The XamlObjectReader's schema context throws this when GetXamlType(Type) cannot find a XamlType for the given CLR type: the schema context does not allow that type. This means the type is outside the set of types the reader's SchemaContext is configured to load (e.g. type not in the local assembly context's allowed assemblies).

Solutions

  1. Add the type's assembly to the schema context's allowed assemblies (or use a full-trust/default XamlSchemaContext)
  2. Use XamlSchemaContext with appropriate XamlSchemaContextSettings/assembly resolution so the type resolves
  3. Replace out-of-context types in the graph with allowed ones before reading
  4. Catch XamlObjectReaderException and fall back to a context configured for that type

Example fix

// before
var sc = new XamlSchemaContext(new[] { typeof(MainWindow).Assembly });
// after: include the assembly defining the type
var sc = new XamlSchemaContext(new[] { typeof(MainWindow).Assembly, typeof(Foreign.Widget).Assembly });
Defensive patterns

Strategy: try-catch

Validate before calling

var sc = new XamlSchemaContext(relevantAssemblies);
var xt = sc.GetXamlType(clrType);
if (xt == null) throw new InvalidOperationException($"{clrType} not resolvable in schema context");

Type guard

bool TypeAllowed(XamlSchemaContext sc, Type t) => sc.GetXamlType(t) != null;

Try / catch

try { var xt = reader.SchemaContext.GetXamlType(clrType); } catch (XamlObjectReaderException ex) when (ex.Message.Contains("TypeNotAllowed")) { /* widen context assembly list */ }

Prevention

When it happens

Trigger: XamlObjectReader.SchemaContext.GetXamlType(clrType) called (directly or indirectly) with a CLR type that the context's assembly set/permission set does not include — e.g. types outside LocalAssembly contexts or non-XAML-compatible types.

Common situations: Partial-trust XAML loading where the type's assembly is not in the allowed list; using XamlObjectReader with a restricted schema context while the graph contains types from other assemblies; plugin assemblies not registered with the context.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/XamlObjectReader.cs:2642

                    }

                    if (prefix.Length == 0)
                    {
                        XmlConvert.VerifyNCName(prefix);
                    }
                }

                namespaceToPrefixMap.Add(ns, prefix);
                prefixToNamespaceMap.Add(prefix, ns);
                return prefix;
            }

            public XamlType GetXamlType(Type clrType)
            {
                XamlType result = schemaContext.GetXamlType(clrType);
                if (result is null)
                {
                    throw new XamlObjectReaderException(SR.Format(SR.ObjectReaderTypeNotAllowed,
                        schemaContext.GetType(), clrType));
                }

                return result;
            }

            public XamlType LocalAssemblyAwareGetXamlType(Type clrType)
            {
                XamlType result = GetXamlType(clrType);
                if (!result.IsVisibleTo(LocalAssembly) && !typeof(Type).IsAssignableFrom(clrType))
                {
                    throw new XamlObjectReaderException(SR.Format(SR.ObjectReader_TypeNotVisible, clrType.FullName));
                }

                return result;
            }

            public bool CanConvertTo(TypeConverter converter, Type type)

View on GitHub (pinned to 81131a70a4)