{"record":{"id":"552f453171752a0d","repo":"dotnet/wpf","slug":"sr-format-sr-mustbeoftype-nameof-value-nameof-typeextension","errorCode":null,"errorMessage":"SR.Format(SR.MustBeOfType, nameof(value), nameof(TypeExtension))","messagePattern":"SR\\.Format\\(SR\\.MustBeOfType, nameof\\(value\\), nameof\\(TypeExtension\\)\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtensionConverter.cs","lineNumber":32,"sourceCode":"#pragma warning restore CA1812\n    {\n        public override bool CanConvertTo(ITypeDescriptorContext context, Type destinationType)\n        {\n            if (destinationType == typeof(InstanceDescriptor))\n            {\n                return true;\n            }\n\n            return base.CanConvertTo(context, destinationType);\n        }\n\n        public override object ConvertTo(ITypeDescriptorContext context, CultureInfo culture, object value, Type destinationType)\n        {\n            if (destinationType == typeof(InstanceDescriptor))\n            {\n                if (!(value is TypeExtension typeExtension))\n                {\n                    throw new ArgumentException(SR.Format(SR.MustBeOfType, nameof(value), nameof(TypeExtension)));\n                }\n\n                return new InstanceDescriptor(\n                    typeof(TypeExtension).GetConstructor(new Type[] { typeof(Type) }),\n                    new object[] { typeExtension.Type }\n                );\n            }\n\n            return base.ConvertTo(context, culture, value, destinationType);\n        }\n    }\n}\n","sourceCodeStart":14,"sourceCodeEnd":45,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtensionConverter.cs#L14-L45","documentation":"TypeExtensionConverter.ConvertTo throws ArgumentException when the value being converted is not a TypeExtension instance. The converter can only produce an InstanceDescriptor from an actual TypeExtension; any other value fails the type check with SR.MustBeOfType naming the parameter and expected type.","triggerScenarios":"Calling TypeConverter.ConvertTo(value, typeof(InstanceDescriptor)) where value is null or an object that is not TypeExtension — e.g. passing the raw Type instead of the extension wrapper.","commonSituations":"Custom designers/codegen calling the converter with the wrong object; serialization pipelines that pass property values of mixed types through a converter registered for TypeExtension.","solutions":["Pass the TypeExtension instance itself, not its underlying Type.","Check value is TypeExtension before calling ConvertTo.","If you have a Type, wrap it: new TypeExtension(theType) then convert.","Use the correct converter for the value's actual runtime type."],"exampleFix":"// before\nvar desc = converter.ConvertTo(null, culture, typeof(string), typeof(InstanceDescriptor)); // throws\n// after\nvar ext = new TypeExtension(typeof(string));\nvar desc = converter.ConvertTo(null, culture, ext, typeof(InstanceDescriptor));","handlingStrategy":"type-guard","validationCode":"if (value is not TypeExtension) throw new ArgumentException($\"Expected TypeExtension, got {value?.GetType().Name ?? \"null\"}.\");","typeGuard":"bool IsTypeExtension(object? v) => v is TypeExtension;","tryCatchPattern":"try { desc = converter.ConvertTo(ctx, culture, value, typeof(InstanceDescriptor)); }\ncatch (ArgumentException ex) { log.Error(ex); desc = null; }","preventionTips":["Only feed a TypeConverter the object types it is registered for.","When serializing, pass the extension object, not the resolved Type."],"tags":["xaml","wpf","type-converter","argument","type-mismatch"],"backgroundTag":"type-mismatch","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"}