{"record":{"id":"88d83e85d52e9540","repo":"dotnet/wpf","slug":"sr-objectwritertypenotallowed","errorCode":null,"errorMessage":"SR.ObjectWriterTypeNotAllowed","messagePattern":"SR\\.ObjectWriterTypeNotAllowed","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs","lineNumber":1087,"sourceCode":"        /// </summary>\n        private object GetKeyFromInstance(object instance, XamlType instanceType, IAddLineInfo lineInfo)\n        {\n            XamlMember keyProperty = instanceType.GetAliasedProperty(XamlLanguage.Key);\n            if (keyProperty is null || instance is null)\n            {\n                throw lineInfo.WithLineInfo(new XamlObjectWriterException(SR.Format(SR.MissingKey, instanceType.Name)));\n            }\n\n            object key = Runtime.GetValue(instance, keyProperty);\n            return key;\n        }\n\n        private XamlType GetXamlType(Type clrType)\n        {\n            XamlType result = SchemaContext.GetXamlType(clrType);\n            if (result is null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.ObjectWriterTypeNotAllowed,\n                    SchemaContext.GetType(), clrType));\n            }\n\n            return result;\n        }\n\n        // These are the all the directives that affect Construction of object.\n        private bool IsConstructionDirective(XamlMember xamlMember)\n        {\n            return xamlMember == XamlLanguage.Arguments\n                || xamlMember == XamlLanguage.Base\n                || xamlMember == XamlLanguage.FactoryMethod\n                || xamlMember == XamlLanguage.Initialization\n                || xamlMember == XamlLanguage.PositionalParameters\n                || xamlMember == XamlLanguage.TypeArguments;\n        }\n\n        // BAML sometimes sends the x:base directive later than it should","sourceCodeStart":1069,"sourceCodeEnd":1105,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Xaml/InfosetObjects/XamlObjectWriter.cs#L1069-L1105","documentation":"InvalidOperationException thrown by XamlObjectWriter.GetXamlType when SchemaContext.GetXamlType(clrType) returns null, i.e. the schema context has no XAML type mapping for the given CLR type. The object writer cannot serialize/instantiate an unmapped type, so it fails with a message naming the schema context and the offending type.","triggerScenarios":"Calling GetXamlType (reached from WriteStartObject root-instance handling and other paths) with a CLR type that the XamlSchemaContext does not map — e.g. types outside the loaded assemblies, non-public types, or types not visible to the context's assembly list.","commonSituations":"Using a restricted XamlSchemaContext that only knows a subset of assemblies; loading XAML in a trimmed/AOT-published app where the type's assembly was cut; internal or private types without proper accessibility for the schema context; assembly version changes renaming or removing the type.","solutions":["Register the CLR type with the schema context (e.g. use a XamlSchemaContext whose reference assemblies include the type's assembly, or a custom XamlSchemaContext with GetXamlType override).","Make the type public with a parameterless constructor so the default type mapping can find it.","Pass the correct assemblies to XamlSchemaContext(IEnumerable<Assembly>) so the type is resolvable.","Verify with SchemaContext.GetXamlType(clrType) != null before writing objects of that type."],"exampleFix":"// before\nvar sc = new XamlSchemaContext(); // does not know MyAssembly\nwriter.GetXamlType(typeof(MyControls.CustomControl)); // InvalidOperationException\n// after\nvar sc = new XamlSchemaContext(new[] { typeof(MyControls.CustomControl).Assembly });\nvar xt = sc.GetXamlType(typeof(MyControls.CustomControl)); // non-null","handlingStrategy":"validation","validationCode":"if (schemaContext.GetXamlType(clrType) == null)\n    throw new InvalidOperationException($\"{clrType} is not mapped in {schemaContext.GetType()}.\");","typeGuard":"static bool IsMapped(XamlSchemaContext sc, Type t) => sc.GetXamlType(t) != null;","tryCatchPattern":"try { processType(clrType); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"not allowed\")) { RegisterAssemblyAndRetry(clrType); }","preventionTips":["List all assemblies containing serializable types in the XamlSchemaContext constructor.","Keep types public with parameterless constructors for default mapping.","Verify type mapping early (startup) rather than mid-load.","Watch for trimmed/AOT builds dropping assemblies the schema context needs."],"tags":["xaml","invalidoperationexception","schema-context","type-mapping"],"backgroundTag":"incompatible-source-type","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"}