{"record":{"id":"c82c06747106a356","repo":"dotnet/wpf","slug":"sr-markupextensiontypename","errorCode":null,"errorMessage":"SR.MarkupExtensionTypeName","messagePattern":"SR\\.MarkupExtensionTypeName","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs","lineNumber":63,"sourceCode":"        /// <summary>\n        /// Return an object that should be set on the targetObject's targetProperty\n        /// for this markup extension. TypeExtension resolves a string into a Type\n        /// and returns it.\n        /// </summary>\n        /// <param name=\"serviceProvider\">Object that can provide services for the markup extension.</param>\n        /// <returns>The object to set on this property.</returns>\n        public override object ProvideValue(IServiceProvider serviceProvider)\n        {\n            // If a type was supplied, no context nor type name are needed\n            if (_type is not null)\n            {\n                return _type;\n            }\n\n            // Validate the initialization.\n            if (_typeName is null)\n            {\n                throw new InvalidOperationException(SR.MarkupExtensionTypeName);\n            }\n\n            // Get the IXamlTypeResolver from the service provider\n            ArgumentNullException.ThrowIfNull(serviceProvider);\n\n            if (serviceProvider.GetService(typeof(IXamlTypeResolver)) is not IXamlTypeResolver xamlTypeResolver)\n            {\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionNoContext, GetType().Name, nameof(IXamlTypeResolver)));\n            }\n\n            // Get the type\n            _type = xamlTypeResolver.Resolve(_typeName);\n            if (_type is null)\n            {\n                throw new InvalidOperationException(SR.Format(SR.MarkupExtensionTypeNameBad, _typeName));\n            }\n\n            return _type;","sourceCodeStart":45,"sourceCodeEnd":81,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/TypeExtension.cs#L45-L81","documentation":"TypeExtension.ProvideValue throws InvalidOperationException when neither a Type nor a TypeName string was supplied before evaluation. The extension cannot resolve a type without one of these, so the library fails fast instead of returning a meaningless value. It is the XAML authoring equivalent of an unset required field.","triggerScenarios":"Calling new TypeExtension().ProvideValue(serviceProvider) without calling the TypeExtension(Type) constructor, without setting Type, and without setting TypeName.","commonSituations":"Hand-constructing TypeExtension in code (rather than from XAML markup) and forgetting to set Type or TypeName; data-binding or tooling code that builds markup extensions dynamically and passes incomplete initialization state.","solutions":["Pass a Type to the constructor: new TypeExtension(typeof(MyType)) before calling ProvideValue.","Set the TypeName property to a valid qualified type-name string, e.g. new TypeExtension { TypeName = \"sys:String\" }.","Set the Type property instead when the CLR type is known at compile time.","Guard the call: only invoke ProvideValue after confirming Type != null || TypeName != null."],"exampleFix":"// before\nvar ext = new TypeExtension();\nvar val = ext.ProvideValue(provider); // throws\n// after\nvar ext = new TypeExtension(typeof(System.String));\nvar val = ext.ProvideValue(provider);","handlingStrategy":"validation","validationCode":"if (ext.Type is null && ext.TypeName is null)\n    throw new InvalidOperationException(\"TypeExtension requires Type or TypeName before ProvideValue.\");","typeGuard":"bool IsInitialized(TypeExtension e) => e is { Type: not null } || e is { TypeName: not null };","tryCatchPattern":"try { value = ext.ProvideValue(provider); }\ncatch (InvalidOperationException ex) { log.Warn(ex); value = Type.GetType(ext.TypeName ?? \"\", throwOnError: false); }","preventionTips":["Always construct TypeExtension with typeof(T) when the type is known at compile time.","Never call ProvideValue manually outside a XAML parse without full initialization.","Add asserts in tooling that builds markup extensions dynamically."],"tags":["xaml","wpf","invalid-operation","markup-extension"],"backgroundTag":"missing-required-argument","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"}