{"record":{"id":"03fceae26f598d57","repo":"dotnet/wpf","slug":"sr-musthavename-reference","errorCode":null,"errorMessage":"SR.MustHaveName","messagePattern":"SR\\.MustHaveName","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/Reference.cs","lineNumber":35,"sourceCode":"        public Reference(string name)\n        {\n            Name = name;\n        }\n\n        [ConstructorArgument(\"name\")]\n        public string Name { get; set; }\n\n        public override object ProvideValue(IServiceProvider serviceProvider)\n        {\n            ArgumentNullException.ThrowIfNull(serviceProvider);\n            if (serviceProvider.GetService(typeof(IXamlNameResolver)) is not IXamlNameResolver nameResolver)\n            {\n                throw new InvalidOperationException(SR.MissingNameResolver);\n            }\n\n            if (string.IsNullOrEmpty(Name))\n            {\n                throw new InvalidOperationException(SR.MustHaveName);\n            }\n\n            object obj = nameResolver.Resolve(Name);\n            if (obj is null)\n            {\n                string[] names = new string[] { Name };\n                obj = nameResolver.GetFixupToken(names, true);\n            }\n\n            return obj;\n        }\n    }\n}\n","sourceCodeStart":17,"sourceCodeEnd":49,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/System.Xaml/System/Windows/Markup/Reference.cs#L17-L49","documentation":"ReferenceExtension.ProvideValue throws InvalidOperationException (SR.MustHaveName) when the extension's Name property is null or empty. {x:Reference} requires a non-empty name to pass to IXamlNameResolver.Resolve. The check runs after the name-resolver service is obtained, before any lookup.","triggerScenarios":"Creating a ReferenceExtension via the parameterless constructor (or with Name = null/\"\") and calling ProvideValue without setting Name.","commonSituations":"XAML with <x:Reference> missing the Name attribute; dynamic construction of the extension where the name binding resolved to an empty string; template/generated XAML with unfilled placeholders.","solutions":["Set Name (or use the ReferenceExtension(string) constructor) with a non-empty value before ProvideValue.","In XAML, always supply the Name attribute on x:Reference elements.","Guard dynamic scenarios: skip creating the extension when the source name is empty."],"exampleFix":"// before\nvar ext = new ReferenceExtension();\nvar v = ext.ProvideValue(provider); // throws: Name is null\n// after\nvar ext = new ReferenceExtension(\"btnOk\");\nvar v = ext.ProvideValue(provider);","handlingStrategy":"validation","validationCode":"if (string.IsNullOrEmpty(ext.Name))\n    throw new InvalidOperationException(\"ReferenceExtension.Name must be a non-empty string before ProvideValue\");","typeGuard":"bool IsUsableReference(ReferenceExtension ext) => ext is not null && !string.IsNullOrEmpty(ext.Name);","tryCatchPattern":"try { var v = ext.ProvideValue(provider); }\ncatch (InvalidOperationException) { /* Name unset — supply or skip the reference */ }","preventionTips":["Use the ReferenceExtension(string) constructor so Name cannot be forgotten.","Validate XAML sources so every x:Reference carries a Name attribute.","In generated XAML, substitute or remove placeholders whose names resolve to empty."],"tags":["empty-value","xaml","xreference","required-property"],"backgroundTag":"empty-required-field","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"}