{"record":{"id":"10b628963d060115","repo":"dotnet/wpf","slug":"no-message-parameterless-invalidoperationexception","errorCode":null,"errorMessage":"(no message - parameterless InvalidOperationException)","messagePattern":"\\(no message - parameterless InvalidOperationException\\)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs","lineNumber":1526,"sourceCode":"        private class TypeValueSerializer : ValueSerializer\n        {\n            private Scope _scope;\n\n            public TypeValueSerializer(Scope scope)\n            {\n                _scope = scope;\n            }\n\n            public override bool CanConvertToString(object value, IValueSerializerContext context)\n            {\n                return true;\n            }\n\n            public override string ConvertToString(object value, IValueSerializerContext context)\n            {\n                Type type = value as Type;\n                if (type == null)\n                    throw new InvalidOperationException();\n                string uri = _scope.MakeAddressable(type);\n                string prefix = _scope.GetPrefixOf(uri);\n                if (prefix == null || prefix == \"\")\n                    return type.Name;\n                else\n                    return $\"{prefix}:{type.Name}\";\n            }\n\n            public override IEnumerable<Type> TypeReferences(object value, IValueSerializerContext context)\n            {\n                Type type = value as Type;\n                if (type != null)\n                    return new Type[] { type };\n                else\n                    return base.TypeReferences(value, context);\n            }\n        }\n","sourceCodeStart":1508,"sourceCodeEnd":1544,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Markup/Primitives/MarkupWriter.cs#L1508-L1544","documentation":"MarkupWriter's internal value serializer converts a Type value to its XML namespace-qualified string form, but throws a parameterless InvalidOperationException when the value passed in is not a Type. This is an internal invariant violation: the serializer was invoked with a value it was never designed to handle.","triggerScenarios":"Calling MarkupWriter/MarkupObject serialization APIs (e.g. MarkupWriter.GetObjectMarkup) or the XAML serializer pipeline where a member's value is null or not a System.Type when ConvertToString is dispatched for the x:Type-like value serializer.","commonSituations":"Serializing an object graph whose property typed/declared as Type is actually null or holds a non-Type instance; custom markup extensions or value serializer plumbing feeding unexpected values into the writer.","solutions":["Inspect the object being serialized and ensure every property that flows through the type serializer contains a non-null System.Type instance","Check for nulls earlier in the object graph before calling MarkupWriter","Wrap serialization in try/catch to log the offending property path","If serializing custom types, register a proper value serializer that handles your value type"],"exampleFix":"// before\nvar markup = MarkupWriter.GetObjectMarkup(obj); // obj.SomeTypeProperty == null\n// after\nif (obj.SomeTypeProperty == null)\n    throw new InvalidOperationException(\"obj.SomeTypeProperty must be a Type before serialization\");\nvar markup = MarkupWriter.GetObjectMarkup(obj);","handlingStrategy":"validation","validationCode":"if (value is not Type t || t == null)\n    throw new InvalidOperationException(\"Expected a System.Type value before MarkupWriter serialization\");","typeGuard":"bool IsType(object v) => v is Type;","tryCatchPattern":"try { var markup = MarkupWriter.GetObjectMarkup(obj); }\ncatch (InvalidOperationException ex) { Log(ex); /* inspect obj graph for null/non-Type members */ }","preventionTips":["Ensure Type-typed properties are never null before serialization","Unit-test serialization of your object graph","Log property paths when serializing custom markup"],"tags":["wpf","xaml","serialization","invalid-operation"],"backgroundTag":"internal-invariant-violation","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"}