{"record":{"id":"d8e07557905d6b96","repo":"dotnet/wpf","slug":"sr-format-sr-markupwriter-cannotserializenonpublictype-type","errorCode":null,"errorMessage":"SR.Format(SR.MarkupWriter_CannotSerializeNonPublictype, type.ToString())","messagePattern":"SR\\.Format\\(SR\\.MarkupWriter_CannotSerializeNonPublictype, type\\.ToString\\(\\)\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs","lineNumber":147,"sourceCode":"        /// <summary>\n        /// Throws an exception if the given type cannot be serialized because it is\n        /// 1) not public\n        /// 2) a nested-public\n        /// 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()","sourceCodeStart":129,"sourceCodeEnd":165,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs#L129-L165","documentation":"MarkupWriter.VerifyTypeIsSerializable throws this InvalidOperationException when the type being serialized is not publicly visible (!type.IsPublic). The XAML serializer can only emit output for public, top-level types, since the generated markup must be resolvable back to the type. Non-public (internal, private) types cannot be round-tripped in XAML.","triggerScenarios":"Calling XamlWriter.Save / MarkupWriter.WriteItem on an object whose runtime type is internal or otherwise non-public; VerifyTypeIsSerializable checks type.IsPublic after excluding nested-public types.","commonSituations":"Serializing internal data/model classes (common when the model lives in a shared assembly marked internal), anonymous types, or types declared with default (internal) accessibility in C#.","solutions":["Make the type public (top-level public class).","Expose a public wrapper/DTO type and copy the data into it before saving.","Serialize with DataContractSerializer or another serializer that supports non-public types."],"exampleFix":"// before\nclass MyData { public string Name { get; set; } } // internal\nXamlWriter.Save(new MyData()); // InvalidOperationException: CannotSerializeNonPublictype\n// after\npublic class MyData { public string Name { get; set; } }\nXamlWriter.Save(new MyData());","handlingStrategy":"validation","validationCode":"static bool IsXamlSerializable(Type t) =>\n    t != null && !t.IsNested && t.IsPublic && !t.IsGenericType;","typeGuard":"static bool IsPublicTopLevel(Type t) => t.IsPublic && !t.IsNested;","tryCatchPattern":"try { XamlWriter.Save(obj, stream); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"non-public\"))\n{\n    // map to a public DTO and retry\n}","preventionTips":["Apply public accessibility to all model classes intended for XAML round-trip.","Watch for internal types in shared assemblies - a common culprit.","Lint model assemblies for non-public types reachable from serialized object graphs."],"tags":["wpf","xaml","serialization","reflection"],"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"}