{"record":{"id":"5533e6c644482a0f","repo":"dotnet/wpf","slug":"sr-format-sr-textattributevaluewrongtype-attribute-ai-type","errorCode":null,"errorMessage":"SR.Format(SR.TextAttributeValueWrongType, attribute, ai.Type.Name, value.GetType().Name)","messagePattern":"SR\\.Format\\(SR\\.TextAttributeValueWrongType, attribute, ai\\.Type\\.Name, value\\.GetType\\(\\)\\.Name\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/Text/TextRange.cs","lineNumber":185,"sourceCode":"        /// attribute.  For example when searching for font size you must specify the size in points as a double.\n        /// If you specify the point size as an integer then you will never get any matches due to the differing types.</param>\n        /// <param name=\"backward\">true if the last occurring range should be returned instead of the first.</param>\n        /// <returns>A subrange with the specified attribute, or null if no such subrange exists.</returns>\n        public TextPatternRange FindAttribute(AutomationTextAttribute attribute, object value, bool backward)\n        {\n            ArgumentNullException.ThrowIfNull(attribute);\n            ArgumentNullException.ThrowIfNull(value); // no text attributes can have null as a valid value\n\n            // Check that attribute value is of expected type...\n            AutomationAttributeInfo ai;\n            if(!Schema.GetAttributeInfo(attribute, out ai))\n            {\n                throw new ArgumentException(SR.UnsupportedAttribute);\n            }\n\n            if (value.GetType() != ai.Type)\n            {\n                throw new ArgumentException(SR.Format(SR.TextAttributeValueWrongType, attribute, ai.Type.Name, value.GetType().Name), nameof(value));\n            }\n\n            // note: if we implement attributes whose values are logical elements, patterns,\n            // or ranges then we'll need to unwrap the objects here before passing them on to\n            // the provider.\n            if (attribute == TextPattern.CultureAttribute)\n            {\n                if (value is CultureInfo)\n                {\n                    value = ((CultureInfo)value).LCID;\n                }\n            }\n\n            SafeTextRangeHandle hResultTextRange = UiaCoreApi.TextRange_FindAttribute(_hTextRange, attribute.Id, value, backward);\n            return Wrap(hResultTextRange, _pattern);\n        }\n\n        /// <summary>","sourceCodeStart":167,"sourceCodeEnd":203,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/Text/TextRange.cs#L167-L203","documentation":"FindAttribute validates that the search value's runtime type exactly matches the type registered for the attribute in the TextPattern schema (ai.Type). When the value type differs, the library throws ArgumentException naming the expected and actual types. This prevents sending a provider an attribute value it cannot interpret.","triggerScenarios":"Calling FindAttribute with a value whose GetType() is not exactly the attribute's declared type, e.g. passing an int boxed value for TextPattern.ForegroundColorAttribute (expects Color), a string for an attribute expecting double (IsHorizontalRulerAttribute-like boolean/double attributes), or a double where float is expected.","commonSituations":"Passing numeric literals (int) where the schema expects double or Color; using string values for enum-typed attributes instead of the enum itself; locale changes where CultureAttribute gets a string instead of CultureInfo.","solutions":["Cast or construct the value to the exact documented attribute type before calling FindAttribute.","For ColorAttribute use System.Windows.Media.Color; for CultureAttribute use CultureInfo; for font-size/weight use the documented primitive type.","Check the attribute's expected type via documentation or Schema, and validate with a type check before the call."],"exampleFix":"// before\nrange.FindAttribute(TextPattern.FontSizeAttribute, 12, false); // int\n// after\nrange.FindAttribute(TextPattern.FontSizeAttribute, 12.0, false); // double","handlingStrategy":"type-guard","validationCode":"bool ok = value.GetType() == expectedTypeFor(attribute); // map each TextPattern attribute to its documented CLR type before calling FindAttribute","typeGuard":"static bool IsCorrectValueType(AutomationTextAttribute a, object v) =>\n    (a == TextPattern.FontSizeAttribute && v is double) ||\n    (a == TextPattern.BackgroundColorAttribute && v is System.Windows.Media.Color) ||\n    (a == TextPattern.CultureAttribute && v is System.Globalization.CultureInfo) ||\n    (a == TextPattern.FontWeightAttribute && v is int);\n// use: if (IsCorrectValueType(attr, value)) range.FindAttribute(attr, value, false);","tryCatchPattern":"try { return range.FindAttribute(attribute, value, backwards); }\ncatch (ArgumentException ex) { throw new InvalidOperationException($\"Value for {attribute.ProgrammaticName} must be of the documented type\", ex); }","preventionTips":["Use double literals (12.0) for FontSizeAttribute, not ints","Box enum/int attribute values as int explicitly when required","Consult the TextPattern attribute documentation table for the exact value type"],"tags":["uiautomation","textpattern","type-mismatch","argument-exception"],"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"}