dotnet/wpf · error · ArgumentException

SR.RuntimeTypeRequired

Error message

SR.RuntimeTypeRequired: {0} (parameter type)

What it means

RequireRuntimeType rejects System.Type instances that are not RuntimeType (the internal concrete type of typeof(...)) to prevent injection of derived Types that spoof identity, since S.W.M.XamlReader only supports live reflection. A non-runtime Type triggers ArgumentException(SR.RuntimeTypeRequired) naming the offending type and the 'type' parameter.

Solutions

  1. Load the assembly normally (Assembly.Load) and pass typeof(T) or a Type obtained from the live runtime, not reflection-only or mocked Types.
  2. Replace TypeDelegator/custom Type subclasses with the real RuntimeType.
  3. In tests, use real compiled types instead of mocking frameworks' fakes of Type.
  4. Guard with `type.GetType() == typeof(object).GetType()` (RuntimeType check) before calling.

Example fix

// before
var fake = new Mock<Type>(); var xt = ctx.GetXamlType(fake.Object);
// after
var xt = ctx.GetXamlType(typeof(MyControl)); // live RuntimeType only
Defensive patterns

Strategy: type-guard

Validate before calling

static bool IsRuntimeType(Type t) => t.GetType() == typeof(object).GetType(); // RuntimeType
if (!IsRuntimeType(type)) throw new ArgumentException("Only live RuntimeType instances are supported.");

Type guard

bool IsRuntimeType(Type t) => t.GetType() == typeof(object).GetType();

Try / catch

try { xt = ctx.GetXamlType(type); }
catch (ArgumentException ex) when (ex.ParamName == "type")
{
    // resolve the real runtime Type via Assembly.Load and retry
}

Prevention

When it happens

Trigger: Calling GetXamlType(Type) (via the RequireRuntimeType guard) with a Type that is not a RuntimeType — e.g. a Type from a reflection-only context, a TypeDelegator/mock, a serialized/remote Type, or a build-time fake.

Common situations: Tools using reflection-only loading or metadata-only assemblies that then query the WPF schema context; unit tests passing mocked Type objects; cross-AppDomain/remoting scenarios handing back non-runtime Type proxies.

Understand the failure class

Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.

Related errors


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

Appendix: source

Thrown at src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfSharedXamlSchemaContext.cs:49

                    {
                        xType = new WpfXamlType(type, this, false /* isBamlType */, _useV3Rules);
                    }
                    _masterTypeTable.Add(type, xType);
                }
            }

            return xType;
        }

        internal static void RequireRuntimeType(Type type)
        {
            // To avoid injection of derived System.Types that lie about their identity
            // (and spoof other types), only allow RuntimeTypes.
            // S.W.M.XamlReader only supports live reflection, anyway.
            Type runtimeType = typeof(object).GetType();
            if (!runtimeType.IsAssignableFrom(type.GetType()))
            {
                throw new ArgumentException(SR.Format(SR.RuntimeTypeRequired, type), nameof(type));
            }
        }

        // Allow wrapping SchemaContexts a way to call into the protected overload of GetXamlType
        internal XamlType GetXamlTypeInternal(string xamlNamespace, string name, params XamlType[] typeArguments)
        {
            return base.GetXamlType(xamlNamespace, name, typeArguments);
        }

        private Dictionary<Type, XamlType> _masterTypeTable = new Dictionary<Type, XamlType>();
        private readonly object _syncObject = new Object();
        private bool _useV3Rules;
    }
}

View on GitHub (pinned to 81131a70a4)