{"record":{"id":"9a9e95b9dc3e07ed","repo":"dotnet/wpf","slug":"sr-format-sr-propertyconditionincorrecttype-property-9a9e95","errorCode":null,"errorMessage":"SR.Format(SR.PropertyConditionIncorrectType, property.ProgrammaticName, expectedType.Name)","messagePattern":"SR\\.Format\\(SR\\.PropertyConditionIncorrectType, property\\.ProgrammaticName, expectedType\\.Name\\)","errorType":"validation","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/PropertyCondition.cs","lineNumber":138,"sourceCode":"        private void Init(AutomationProperty property, object val, PropertyConditionFlags flags )\n        {\n            ArgumentNullException.ThrowIfNull(property);\n\n            AutomationPropertyInfo info;\n            if (!Schema.GetPropertyInfo(property, out info))\n            {\n                throw new ArgumentException(SR.UnsupportedProperty);\n            }\n\n            // Check type is appropriate: NotSupported is allowed against any property,\n            // null is allowed for any reference type (ie not for value types), otherwise\n            // type must be assignable from expected type.\n            Type expectedType = info.Type;\n            if (val != AutomationElement.NotSupported &&\n                ((val == null && expectedType.IsValueType)\n                || (val != null && !expectedType.IsAssignableFrom(val.GetType()))))\n            {\n                throw new ArgumentException(SR.Format(SR.PropertyConditionIncorrectType, property.ProgrammaticName, expectedType.Name));\n            }\n\n            if ((flags & PropertyConditionFlags.IgnoreCase) != 0)\n            {\n                Misc.ValidateArgument(val is string, nameof(SR.IgnoreCaseRequiresString));\n            }\n\n            // Some types are handled differently in managed vs unmanaged - handle those here...\n            if (val is AutomationElement)\n            {\n                // If this is a comparison against a Raw/LogicalElement,\n                // save the runtime ID instead of the element so that we\n                // can take it cross-proc if needed.\n                val = ((AutomationElement)val).GetRuntimeId();\n            }\n            else if (val is ControlType)\n            {\n                // If this is a control type, use the ID, not the CLR object","sourceCodeStart":120,"sourceCodeEnd":156,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/UIAutomation/UIAutomationClient/System/Windows/Automation/PropertyCondition.cs#L120-L156","documentation":"The PropertyCondition constructor checks that the condition value's type matches the property's expected type: null is rejected for value-type properties, and the value's runtime type must be assignable to the expected type. On mismatch it throws ArgumentException naming the property's programmatic name and the expected type.","triggerScenarios":"new PropertyCondition(boolProperty, \"true\"); new PropertyCondition(numericProperty, null); passing a string value for a double/enum/int property in FindAll/FindFirst conditions.","commonSituations":"Values sourced from UI text boxes or configuration as strings; JSON-deserialized values of indeterminate type; switching a property whose expected type changed between UIA versions; accidentally passing the wrong enum type (e.g. DockPosition as string).","solutions":["Coerce the value to the expected type before constructing (Convert.ChangeType with the property's expected type)","Use proper enum values (e.g. DockPosition.Top) instead of strings","Pass AutomationElement.NotSupported only where semantics allow it, not null for value types","Centralize condition building in a helper that validates value types"],"exampleFix":"// before\nvar cond = new PropertyCondition(AutomationElement.IsOffscreenProperty, \"false\"); // string vs bool\n// after\nvar cond = new PropertyCondition(AutomationElement.IsOffscreenProperty, false);","handlingStrategy":"validation","validationCode":"object Coerce(AutomationProperty p, object v) => v switch {\n    null or object _ when v == AutomationElement.NotSupported => v,\n    string s when p == AutomationElement.IsOffscreenProperty => bool.Parse(s),\n    string s when double.TryParse(s, out var d) => d,\n    _ => v };","typeGuard":"bool TypeOk(Type expected, object v) => v == AutomationElement.NotSupported || (v == null ? !expected.IsValueType : expected.IsInstanceOfType(v));","tryCatchPattern":"try { cond = new PropertyCondition(p, v); } catch (ArgumentException ex) { /* log p.ProgrammaticName; coerce or skip */ }","preventionTips":["Never pass raw strings for bool/numeric/enum properties","Use enum constants (e.g. DockPosition.Top) not names","Centralize condition creation through a type-checking helper"],"tags":["uiautomation","propertycondition","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-22T01:17:13.364Z"}