wmjordan/PDFPatcher · error · ArgumentException
尺寸单位无效。
Error message
尺寸单位无效。
What it means
The UnitConverter.Unit setter looks up the supplied unit string in Constants.Units.Names ([厘米/CM, 毫米/MM, 英寸/Inch, 点/Point]) via ValueHelper.MapValue, which returns the default (0) when no match is found. Because a factor of 0 would silently make every conversion produce 0, the setter treats factor==0 as an invalid unit and throws ArgumentException.
Source
Thrown at App/Model/UnitConverter.cs:41
get => _Precision;
set {
if (value < 0 || value > 6) {
throw new ArgumentException("转换精度不能小于 0 或大于 6。");
}
_Precision = value;
_PreservedValue = (float)Math.Pow(0.1, _Precision);
}
}
private string _Unit;
///<summary>获取或指定转换使用的单位。</summary>
[XmlAttribute("单位")]
public string Unit {
get => _Unit;
set {
var f = ValueHelper.MapValue(value, Constants.Units.Names, Constants.Units.Factors, 0);
if (f == 0) {
throw new ArgumentException("尺寸单位无效。");
}
UnitFactor = f;
_Unit = value;
}
}
public UnitConverter() {
Unit = Constants.Units.CM;
Precision = 3;
}
internal float FromPoint(float point) {
return (point < _PreservedValue && point >= 0) // preserve small fragment
? point
: (float)Math.Round(point / UnitFactor, _Precision);
}
internal float ToPoint(float value) {
View on GitHub (pinned to 4782bbd9ad)
Solutions
- Use exactly one of the four recognized names: "厘米", "毫米", "英寸", or "点" (see Constants.Units.Names).
- If you have an English unit code, map it to the localized name before assigning.
- Validate the unit string against Constants.Units.Names before setting Unit.
- Check the '单位' attribute in the XML info/options document for typos or encoding issues.
Example fix
// before converter.Unit = "cm"; // throws: not in the localized name table // after converter.Unit = Constants.Units.CM; // "厘米"
Defensive patterns
Strategy: validation
Validate before calling
string unit = MapEnglishToLocalized(unitCode);
if (Array.IndexOf(Constants.Units.Names, unit) < 0)
unit = Constants.Units.CM;
converter.Unit = unit; Type guard
static bool IsValidUnit(string u) => Array.IndexOf(Constants.Units.Names, u) >= 0;
Prevention
- Use the Constants.Units.* constants (CM, MM, Inch, Point) instead of string literals.
- Map English unit codes to localized names before assignment.
- Validate the XML '单位' attribute against Constants.Units.Names.
When it happens
Trigger: Assigning UnitConverter.Unit a string that is not one of the four supported localized names ("厘米", "毫米", "英寸", "点"). Also triggered by deserialization of an XML options file whose '单位' attribute is misspelled, in the wrong language, or null/empty.
Common situations: Storing the English abbreviation ('cm','mm','in','pt') instead of the Chinese localized name expected by Constants.Units.Names; a locale mismatch in the options XML; a corrupted or hand-edited settings file.
Related errors
AI-assisted analysis of wmjordan/PDFPatcher@4782bbd9ad (2026-08-13).
Data as JSON: /api/errors/3809d0169d3fb9bd.
Report an issue: GitHub.