{"record":{"id":"089a8705b16b8dc8","repo":"dotnet/wpf","slug":"sr-format-sr-invalidctorparameterunknowngridunittype-type","errorCode":null,"errorMessage":"SR.Format(SR.InvalidCtorParameterUnknownGridUnitType, \"type\")","messagePattern":"SR\\.Format\\(SR\\.InvalidCtorParameterUnknownGridUnitType, \"type\"\\)","errorType":"exception","errorClass":"ArgumentException","httpStatus":null,"severity":"error","filePath":"src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/GridLength.cs","lineNumber":102,"sourceCode":"        /// If <c>value</c> parameter is <c>double.NaN</c>\n        /// or <c>value</c> parameter is <c>double.NegativeInfinity</c>\n        /// or <c>value</c> parameter is <c>double.PositiveInfinity</c>.\n        /// </exception>\n        public GridLength(double value, GridUnitType type)\n        {\n            if (double.IsNaN(value))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoNaN, \"value\"));\n            }\n            if (double.IsInfinity(value))\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterNoInfinity, \"value\"));\n            }\n            if (    type != GridUnitType.Auto\n                &&  type != GridUnitType.Pixel\n                &&  type != GridUnitType.Star   )\n            {\n                throw new ArgumentException(SR.Format(SR.InvalidCtorParameterUnknownGridUnitType, \"type\"));\n            }\n\n            _unitValue = (type == GridUnitType.Auto) ? 0.0 : value;\n            _unitType = type;\n        }\n\n        #endregion Constructors\n\n        //------------------------------------------------------\n        //\n        //  Public Methods\n        //\n        //------------------------------------------------------\n\n        #region Public Methods \n\n        /// <summary>\n        /// Overloaded operator, compares 2 GridLength's.","sourceCodeStart":84,"sourceCodeEnd":120,"githubUrl":"https://github.com/dotnet/wpf/blob/81131a70a4c573cd62748a5c36908fc4d662daa9/src/Microsoft.DotNet.Wpf/src/PresentationFramework/System/Windows/GridLength.cs#L84-L120","documentation":"GridLength's constructor only accepts GridUnitType.Auto, GridUnitType.Pixel, or GridUnitType.Star. Any other GridUnitType value (including undefined numeric casts) fails this guard and throws ArgumentException (SR.InvalidCtorParameterUnknownGridUnitType) because the unit type would have no meaning for layout.","triggerScenarios":"new GridLength(10, (GridUnitType)42), deserializing a persisted enum int that is not 0/1/2, or passing a GridUnitType member added/renamed in another assembly version.","commonSituations":"Round-tripping GridLength settings through config or XAML-adjacent serialization where the enum was stored as a raw int; interop with code that uses different grid-unit constants; enum arithmetic producing out-of-range values.","solutions":["Validate the unit type before constructing: Enum.IsDefined(typeof(GridUnitType), type) and that it is one of Auto/Pixel/Star.","Map unknown values to a safe default (Pixel or Auto) at the deserialization boundary.","Persist GridUnitType as its named string rather than a raw integer.","Audit cast sites: replace (GridUnitType)intValue with an explicit switch that has a default fallback."],"exampleFix":"// before\nvar length = new GridLength(value, (GridUnitType)storedInt);\n\n// after\nvar type = storedInt is >= 0 and <= 2 ? (GridUnitType)storedInt : GridUnitType.Pixel;\nvar length = new GridLength(type == GridUnitType.Auto ? 0 : value, type);","handlingStrategy":"validation","validationCode":"if (type is not (GridUnitType.Auto or GridUnitType.Pixel or GridUnitType.Star))\n    type = GridUnitType.Pixel;\nvar length = new GridLength(value, type);","typeGuard":"bool IsValidGridUnitType(GridUnitType t) => t is GridUnitType.Auto or GridUnitType.Pixel or GridUnitType.Star;","tryCatchPattern":"try { len = new GridLength(value, type); }\ncatch (ArgumentException ex) when (ex.ParamName == \"type\")\n{ len = new GridLength(value, GridUnitType.Pixel); }","preventionTips":["Persist GridUnitType as a string, not an int.","Never cast raw ints to GridUnitType without Enum.IsDefined.","Restrict UI inputs to the three valid unit types via a whitelist."],"tags":["wpf","gridlength","gridunittype","argumentexception","enum"],"backgroundTag":"invalid-enum-value","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"}