Float and Date Parsing must be culture aware
Warns about non-globalized overloads (without an IFormatProvider parameter) of Parse(), TryParse() and ToString() on DateTime, float, double and decimal — a symptom that the application supporting localized UIs and regional data is at least partially not globalized.
How to fix
Use the globalized overloads; culprit callers are listed in MethodsCallingMe. More on Creating Globally Aware Applications: https://msdn.microsoft.com/en-us/library/cc853414(VS.95).aspx
Estimated Debt: 5 minutes per method calling a non-culture aware method, plus 3 minutes per non-culture aware method called.
Estimated Debt: 5 minutes per method calling a non-culture aware method, plus 3 minutes per non-culture aware method called.
CQLinq source
This rule is written in CQLinq, C# LINQ over the NDepend code model. It ships with every new NDepend project: you can read it, adapt it, or use it as a starting point for your own custom rules.
// <Name>Float and Date Parsing must be culture aware</Name>
// <Id>ND2700:FloatAndDateParsingMustBeCultureAware</Id>
warnif count > 0
let cultureUnawareMethods =
(from m in ThirdParty.Types.WithFullNameIn(
"System.DateTime",
"System.Single",
"System.Double",
"System.Decimal").ChildMethods()
where m.NbParameters > 0 &&
(m.SimpleNameEqualsAny(
"Parse", "TryParse", "ToString")) &&
!m.Name.Contains("IFormatProvider")
select m).ToHashSetEx()
from m in JustMyCode.Methods.UsingAny(cultureUnawareMethods)
let cultureUnawareMethodsCalled = m.MethodsCalled.Intersect(cultureUnawareMethods)
select new {
m,
shouldntCall = cultureUnawareMethodsCalled,
Debt = (5 + 3*cultureUnawareMethodsCalled.Count()).ToMinutes().ToDebt(),
Severity = 5*cultureUnawareMethodsCalled.Count().ToMinutes().ToAnnualInterest()
}
//<Expl>
// The *{0}* calls the *{1}* and should use a culture aware version instead.
// This way your application gets globalized.
//</Expl>
//<Desc>
// Warns about *non-globalized overloads* (without an **IFormatProvider**
// parameter) of **Parse()**, **TryParse()** and **ToString()** on
// **DateTime**, **float**, **double** and **decimal** — a symptom that the
// application supporting localized UIs and regional data is *at least
// partially* not globalized.
//</Desc>
//<HowToFix>
// Use the globalized overloads; culprit callers are listed in
// **MethodsCallingMe**. More on **Creating Globally Aware Applications**:
// https://msdn.microsoft.com/en-us/library/cc853414(VS.95).aspx
//
// Estimated Debt: 5 minutes per method calling a non-culture aware method,
// plus 3 minutes per non-culture aware method called.
//</HowToFix>