dotnet/wpf · error · XamlObjectReaderException

SR.ObjectReader_TypeNotVisible

Error message

SR.ObjectReader_TypeNotVisible

What it means

XamlObjectReader throws this when it resolves a CLR type via GetXamlType but that type is not visible to the local assembly being processed (IsVisibleTo(LocalAssembly) fails and the type is not a Type). This guards XAML reflection against non-public types, since serializing/deserializing non-visible types would bypass accessibility rules.

Solutions

  1. Make the referenced CLR type public
  2. Check the XAML for references to types in the local assembly that are internal and expose public wrappers
  3. Verify LocalAssembly is set correctly; if the type genuinely belongs to another visible assembly, ensure the schema context resolves it from the right assembly
  4. If the node intentionally represents a System.Type, use x:Type so the typeof(Type) exemption applies

Example fix

// before
internal class MyConverter { ... }
// after
public class MyConverter { ... }
Defensive patterns

Strategy: validation

Validate before calling

if (!clrType.IsPublic && localAssembly != null && clrType.Assembly == localAssembly && !typeof(Type).IsAssignableFrom(clrType)) throw new InvalidOperationException($"Type {clrType.FullName} is not visible to the local assembly.");

Type guard

bool IsVisibleTo(Type t, Assembly local) => typeof(Type).IsAssignableFrom(t) || (t.IsNested ? t.IsVisible : t.IsPublic);

Prevention

When it happens

Trigger: Calling XamlObjectReader over a node stream or object graph that references an internal/private CLR type while a LocalAssembly is set (e.g. when reading XAML produced by a partial-trust XamlXmlWriter save or within the XamlDesigner factory path).

Common situations: Developer marks a view-model or control class 'internal' but references it from XAML; saving and reloading XAML in a designer/partial-trust sandbox; assembly-split refactors making a type non-public in the local assembly.

Understand the failure class

Background: Permission denied / not authorized / 403 Forbidden: access-control rejections when the caller lacks the required role, grant, or ownership — this error's family across 18 libraries.

Related errors


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

Appendix: source

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

            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)
            {
                return Runtime.CanConvertTo(TypeDescriptorContext, converter, type);
            }

            public bool CanRoundTripString(TypeConverter converter)
            {
                if (converter is ReferenceConverter)
                {
                    return false;
                } // ReferenceConverter lies.

                return Runtime.CanConvertFrom<string>(TypeDescriptorContext, converter) &&

View on GitHub (pinned to 81131a70a4)