{"record":{"id":"8588b7adf13e6a02","repo":"dotnet/wpf","slug":"sr-runtimetyperequired-0-parameter-type","errorCode":null,"errorMessage":"SR.RuntimeTypeRequired: {0} (parameter type)","messagePattern":"SR\\.RuntimeTypeRequired: (.+?) \\(parameter type\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfSharedXamlSchemaContext.cs","lineNumber":49,"sourceCode":"                    {\n                        xType = new WpfXamlType(type, this, false /* isBamlType */, _useV3Rules);\n                    }\n                    _masterTypeTable.Add(type, xType);\n                }\n            }\n\n            return xType;\n        }\n\n        internal static void RequireRuntimeType(Type type)\n        {\n            // To avoid injection of derived System.Types that lie about their identity\n            // (and spoof other types), only allow RuntimeTypes.\n            // S.W.M.XamlReader only supports live reflection, anyway.\n            Type runtimeType = typeof(object).GetType();\n            if (!runtimeType.IsAssignableFrom(type.GetType()))\n            {\n                throw new ArgumentException(SR.Format(SR.RuntimeTypeRequired, type), nameof(type));\n            }\n        }\n\n        // Allow wrapping SchemaContexts a way to call into the protected overload of GetXamlType\n        internal XamlType GetXamlTypeInternal(string xamlNamespace, string name, params XamlType[] typeArguments)\n        {\n            return base.GetXamlType(xamlNamespace, name, typeArguments);\n        }\n\n        private Dictionary<Type, XamlType> _masterTypeTable = new Dictionary<Type, XamlType>();\n        private readonly object _syncObject = new Object();\n        private bool _useV3Rules;\n    }\n}\n","sourceCodeStart":31,"sourceCodeEnd":64,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Baml2006/WpfSharedXamlSchemaContext.cs#L31-L64","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Load the assembly normally (Assembly.Load) and pass typeof(T) or a Type obtained from the live runtime, not reflection-only or mocked Types.","Replace TypeDelegator/custom Type subclasses with the real RuntimeType.","In tests, use real compiled types instead of mocking frameworks' fakes of Type.","Guard with `type.GetType() == typeof(object).GetType()` (RuntimeType check) before calling."],"exampleFix":"// before\nvar fake = new Mock<Type>(); var xt = ctx.GetXamlType(fake.Object);\n// after\nvar xt = ctx.GetXamlType(typeof(MyControl)); // live RuntimeType only","handlingStrategy":"type-guard","validationCode":"static bool IsRuntimeType(Type t) => t.GetType() == typeof(object).GetType(); // RuntimeType\nif (!IsRuntimeType(type)) throw new ArgumentException(\"Only live RuntimeType instances are supported.\");","typeGuard":"bool IsRuntimeType(Type t) => t.GetType() == typeof(object).GetType();","tryCatchPattern":"try { xt = ctx.GetXamlType(type); }\ncatch (ArgumentException ex) when (ex.ParamName == \"type\")\n{\n    // resolve the real runtime Type via Assembly.Load and retry\n}","preventionTips":["Never pass reflection-only, mocked, or TypeDelegator-wrapped Types to the schema context.","Load assemblies with Assembly.Load before querying XAML types.","In tests, use compiled types rather than Type mocks."],"tags":["wpf","xaml","schema-context","reflection","type-safety"],"backgroundTag":"type-mismatch","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-21T21:30:21.729Z"}