{"record":{"id":"daf46a65ff192b58","repo":"dotnet/wpf","slug":"sr-format-sr-valueserializercontextunavailable-this-gettype","errorCode":null,"errorMessage":"SR.Format(SR.ValueSerializerContextUnavailable, this.GetType().Name )","messagePattern":"SR\\.Format\\(SR\\.ValueSerializerContextUnavailable, this\\.GetType\\(\\)\\.Name \\)","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Input/Command/CommandValueSerializer.cs","lineNumber":75,"sourceCode":"        public override string ConvertToString(object value, IValueSerializerContext context)\n        {\n            if (value != null)\n            {\n                RoutedCommand command = value as RoutedCommand;\n                if (null != command && null != command.OwnerType)\n                {\n                    // Known Commands, so write shorter version\n                    if (CommandConverter.IsKnownType(command.OwnerType))\n                    {\n                        return command.Name;\n                    }\n                    else\n                    {\n                        ValueSerializer typeSerializer = null;\n\n                        if (context == null)\n                        {\n                            throw new InvalidOperationException(SR.Format(SR.ValueSerializerContextUnavailable, this.GetType().Name ));\n                        }\n\n                        // Get the ValueSerializer for the System.Type type\n                        typeSerializer = context.GetValueSerializerFor(typeof(Type));\n                        if (typeSerializer == null)\n                        {\n                            throw new InvalidOperationException(SR.Format(SR.TypeValueSerializerUnavailable, this.GetType().Name ));\n                        }\n\n                        return $\"{typeSerializer.ConvertToString(command.OwnerType, context)}.{command.Name}Command\";\n                    }\n                }\n            }\n            else\n                return string.Empty;\n            \n            throw GetConvertToException(value, typeof(string));\n        }","sourceCodeStart":57,"sourceCodeEnd":93,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/Input/Command/CommandValueSerializer.cs#L57-L93","documentation":"CommandValueSerializer.ConvertToString serializes an RoutedCommand as \"ns:Class.NameCommand\", which requires an ITypeDescriptorContext to look up the ValueSerializer for System.Type. When context is null it cannot resolve the owner type and throws InvalidOperationException (SR.ValueSerializerContextUnavailable).","triggerScenarios":"Calling valueSerializer.ConvertToString(routedCommand, null) directly; invoking the serializer outside a XAML/type-descriptor pipeline that normally supplies the context.","commonSituations":"Custom serialization or designer tooling calling ValueSerializer APIs manually without building an ITypeDescriptorContext; unit tests passing null for context; logging code that tries to stringify commands via the serializer.","solutions":["Provide a valid ITypeDescriptorContext (e.g., obtained from TypeDescriptor context or the WPF XAML designer host) instead of null.","Outside XAML serialization, format the command directly: $\"{command.OwnerType.FullName}.{command.Name}Command\".","Guard the call site: skip serializer-based conversion when no context is available."],"exampleFix":"// before\nstring s = serializer.ConvertToString(command, null);\n\n// after\nstring s = command != null\n    ? $\"{command.OwnerType.AssemblyQualifiedName}\" // or manual ns:Class formatting outside XAML\n    : string.Empty;","handlingStrategy":"type-guard","validationCode":"if (context == null)\n    return $\"{command.OwnerType.FullName}.{command.Name}Command\"; // manual fallback\nreturn serializer.ConvertToString(command, context);","typeGuard":"bool CanUseValueSerializer(ITypeDescriptorContext ctx) => ctx != null;","tryCatchPattern":"try { s = serializer.ConvertToString(command, context); }\ncatch (InvalidOperationException ex) when (ex.Message.Contains(\"context\"))\n{ s = $\"{command.OwnerType.FullName}.{command.Name}Command\"; }","preventionTips":["Only call ValueSerializer APIs from within a real XAML/type-descriptor pipeline.","Never pass null as ITypeDescriptorContext to ConvertToString/ConvertFromString.","In tests and tools, build a context object or use manual command formatting instead."],"tags":["wpf","valueserializer","routedcommand","invalidoperationexception","null-context"],"backgroundTag":"null-argument","analyzedSha":"81131a70a4c573cd62748a5c36908fc4d662daa9","analyzedAt":"2026-09-14T10:12:48.479Z","contentChangedAt":"2026-09-14T10:12:48.479Z","schemaVersion":2},"datasetVersion":"2026-09-22T01:17:13.364Z"}