{"record":{"id":"af8ff2cb4d4b0aaa","repo":"dotnet/wpf","slug":"sr-format-sr-markupwriter-cannotserializegenerictype-type","errorCode":null,"errorMessage":"SR.Format(SR.MarkupWriter_CannotSerializeGenerictype, type.ToString())","messagePattern":"SR\\.Format\\(SR\\.MarkupWriter_CannotSerializeGenerictype, type\\.ToString\\(\\)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs","lineNumber":151,"sourceCode":"        /// 3) a generic type\n        /// </summary>\n        /// <param name=\"type\">\n        /// The type to be checked\n        /// </param>\n        internal static void VerifyTypeIsSerializable(Type type)\n        {\n            // Check the type to make sure that it is not a nested type, that it is public, and that it is not generic\n            if (type.IsNestedPublic)\n            {\n                throw new InvalidOperationException( SR.Format( SR.MarkupWriter_CannotSerializeNestedPublictype, type.ToString() ));\n            }\n            if (!type.IsPublic )\n            {\n                throw new InvalidOperationException( SR.Format( SR.MarkupWriter_CannotSerializeNonPublictype, type.ToString() ));\n            }\n            if (type.IsGenericType)\n            {\n                throw new InvalidOperationException( SR.Format( SR.MarkupWriter_CannotSerializeGenerictype, type.ToString() ));\n            }\n        }\n\n        #region Internal Implementation\n        internal MarkupWriter(XmlWriter writer)\n        {\n            _writer = writer;\n            _xmlTextWriter = writer as XmlTextWriter;\n        }\n\n        /// <summary>\n        /// Dispose method\n        /// </summary>\n        public void Dispose()\n        {\n            GC.SuppressFinalize(this);\n        }\n","sourceCodeStart":133,"sourceCodeEnd":169,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs#L133-L169","documentation":"MarkupWriter.VerifyTypeIsSerializable throws this InvalidOperationException when the type is generic (type.IsGenericType). XAML has no representation for constructed generic types in this writer, so it refuses to serialize them rather than emit markup that could not be parsed back.","triggerScenarios":"Calling XamlWriter.Save / MarkupWriter.WriteItem on an instance whose type is a closed generic, e.g. List<MyItem>, KeyValuePair<string,int>, or a custom generic class. VerifyTypeIsSerializable rejects type.IsGenericType after the nested/public checks.","commonSituations":"Saving a List<T> or Dictionary<K,V> collection as the save root with XamlWriter.Save; serializing generic view-model or wrapper types.","solutions":["Serialize the generic collection's elements individually or via a non-generic collection type.","Define a concrete non-generic type (e.g. a class deriving from List<MyItem>) and save that.","Use a different serializer (JSON, DataContractSerializer) that supports generics."],"exampleFix":"// before\nXamlWriter.Save(new List<string> { \"a\" }); // InvalidOperationException: CannotSerializeGenerictype\n// after\npublic class StringList : List<string> { }\nXamlWriter.Save(new StringList { \"a\" });","handlingStrategy":"validation","validationCode":"static bool IsXamlSerializable(Type t) =>\n    t != null && !t.IsNested && t.IsPublic && !t.IsGenericType;","typeGuard":"static bool IsNotGeneric(Type t) => !t.IsGenericType;","tryCatchPattern":"try { XamlWriter.Save(obj, stream); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"generic\"))\n{\n    // fall back to a concrete collection or custom serializer\n}","preventionTips":["Don't pass generic collections (List<T>, Dictionary<K,V>) directly to XamlWriter.Save.","Define concrete subclasses of generic collections for XAML scenarios.","Validate the object graph types with a pre-check before saving."],"tags":["wpf","xaml","serialization","generics"],"backgroundTag":"unsupported-operation","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"}