{"record":{"id":"a700096edbda2798","repo":"dotnet/wpf","slug":"sr-cachedpropertynotrequested","errorCode":null,"errorMessage":"SR.CachedPropertyNotRequested","messagePattern":"SR\\.CachedPropertyNotRequested","errorType":"exception","errorClass":"InvalidOperationException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/AutomationElement.cs","lineNumber":1033,"sourceCode":"        /// Returns the parent of this element, with respect to the TreeFilter\n        /// condition of the CacheRequest that was active when this AutomationElement\n        /// was obtained.\n        /// \n        /// Throws InvalidOperationException if the parent was not previously requested\n        /// in a CacheRequest.\n        /// \n        /// Can return null if the specified element has no parent - eg. is the root node.\n        /// </remarks>\n        public AutomationElement CachedParent\n        {\n            get\n            {\n                // this is used as a marker to indicate 'not requested'\n                // - used since null is a valid value for parent, but this can never be.\n                // Use (object) case to ensure we just do a ref check here, not call .Equals\n                if ((object)_cachedParent == (object)this)\n                {\n                    throw new InvalidOperationException(SR.CachedPropertyNotRequested);\n                }\n\n                return _cachedParent;\n            }\n        }\n\n        /// <summary>\n        /// Returns the cached children of this AutomationElement\n        /// </summary>\n        /// <remarks>\n        /// Returns a collection of children of this element, with respect to the TreeFilter\n        /// condition of the CacheRequest that was active when this AutomationElement\n        /// was obtained.\n        /// \n        /// Throws InvalidOperationException if children or descendants were not previously requested\n        /// in a CacheRequest.\n        /// \n        /// Can return an empty collection if this AutomationElement has no children.","sourceCodeStart":1015,"sourceCodeEnd":1051,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/AutomationElement.cs#L1015-L1051","documentation":"CachedParent is only populated by a CacheRequest that included TreeScope.Parent. The property detects the internal 'not requested' marker (_cachedParent reference-equals this element) and throws InvalidOperationException so callers don't mistake 'not requested' for a real parent value.","triggerScenarios":"Reading element.CachedParent when the element was obtained without a cache request, or with a CacheRequest whose TreeScope did not include Parent (e.g. TreeScope.Element or TreeScope.Children only).","commonSituations":"Accessing .Cached* properties on elements returned from FindFirst/FindAll without adding the required scope; switching from Current* to Cached* properties without setting up AutomationElement.RootElement.CachedParent-style requests.","solutions":["Include TreeScope.Parent in the CacheRequest before building the cached element","Use CurrentParent instead of CachedParent when no caching is intended","Activate a CacheRequest (Push/Pop) around the element retrieval"],"exampleFix":"// before\nvar parent = found.CachedParent; // throws\n// after\nvar request = new CacheRequest { TreeScope = TreeScope.Element | TreeScope.Parent };\nusing (request.Activate())\n{\n    var found = root.FindFirst(TreeScope.Children, condition);\n    var parent = found.CachedParent;\n}","handlingStrategy":"validation","validationCode":"static CacheRequest ParentCachingRequest() =>\n    new CacheRequest { TreeScope = TreeScope.Element | TreeScope.Parent };","typeGuard":"bool HasCachedParent(AutomationElement e) => e is AutomationElement el && !ReferenceEquals(el.CachedParentSafe(), el); // wrap non-throwing accessor","tryCatchPattern":"try { var parent = e.CachedParent; }\ncatch (InvalidOperationException) { parent = e.Current.ParentFallback(); }","preventionTips":["Always build the CacheRequest scope explicitly; include Parent if you read CachedParent","Never assume Cached* values exist without a preceding CacheRequest","Prefer Current* properties unless cross-operation caching is intentional"],"tags":["uiautomation","cache-request","wpf","cached-properties"],"backgroundTag":"cached-property-not-requested","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"}