我重构了一些单元测试,发现某些解析策略依赖于DateTime.TryParseExact
和sbyte.TryPase
,而它们本身依赖于NumberFormatInfo.CurrentInfo
和DateTimeFormatInfo.CurrentInfo
。
为了使我的单元测试正确安排,我决定通过以下方式将CurrentInfo
和NumberFormatInfo
的DateTimeFormatInfo
属性设置为其不变的风格:
CultureInfo.CurrentCulture = CultureInfo.InvariantCulture;
但是,出于好奇,我试图检查是否通过单元测试设置了相应的
InvariantInfo
,结果发现不是,不是。我想知道我在这两个InvariantCulture
中强制执行CurrentInfo
的内容是什么[Fact]
public void ShouldReturnInvariantInfo()
{
CultureInfo.CurrentCulture.NumberFormat = NumberFormatInfo.InvariantInfo;
CultureInfo.CurrentCulture.DateTimeFormat = DateTimeFormatInfo.InvariantInfo;
NumberFormatInfo.CurrentInfo.Should().Be(NumberFormatInfo.InvariantInfo);
DateTimeFormatInfo.CurrentInfo.Should().Be(DateTimeFormatInfo.InvariantInfo);
}
知道底层实现:
NumberFormatInfo.InvariantInfo
:public static NumberFormatInfo CurrentInfo
{
get
{
CultureInfo currentCulture = CultureInfo.CurrentCulture;
if (!currentCulture._isInherited)
{
NumberFormatInfo numInfo = currentCulture.numInfo;
if (numInfo != null)
return numInfo;
}
return (NumberFormatInfo) currentCulture.GetFormat(typeof (NumberFormatInfo));
}
}
DateTimeFormatInfo.CurrentInfo
:public static DateTimeFormatInfo CurrentInfo
{
get
{
CultureInfo currentCulture = CultureInfo.CurrentCulture;
if (!currentCulture._isInherited)
{
DateTimeFormatInfo dateTimeInfo = currentCulture.dateTimeInfo;
if (dateTimeInfo != null)
return dateTimeInfo;
}
return (DateTimeFormatInfo) currentCulture.GetFormat(typeof (DateTimeFormatInfo));
}
}
最佳答案
如果我理解正确,您想在Current
的区域性下运行一些代码(测试),但NumberFormat
和DateTimeFormat
除外。如果是您的情况,建议您Invariant
当前区域性并修改克隆:
// Current culture clone
CultureInfo testCulture = CultureInfo.CurrentCulture.Clone() as CultureInfo;
// modified: current culture except Number and DateTime which are Invariant
testCulture.NumberFormat = CultureInfo.InvariantCulture.NumberFormat;
testCulture.DateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;
// and, finally, set back as current
CultureInfo.CurrentCulture = testCulture;
让我们来看看格式
Console.Write(ReferenceEquals(CultureInfo.CurrentCulture.DateTimeFormat,
CultureInfo.InvariantCulture.DateTimeFormat)
? "Equals"
: "Not Equals");
结果:
Equals
编辑:为了运行代码
Clone
修改后的区域性,我们可以为其实现一个类: public class TestCulture : IDisposable {
private CultureInfo m_SavedCulture;
private CultureInfo m_TestCulture;
private bool m_IsDisposed;
public TestCulture() {
m_SavedCulture = CultureInfo.CurrentCulture;
m_TestCulture = CultureInfo.CurrentCulture.Clone() as CultureInfo;
m_TestCulture.NumberFormat = CultureInfo.InvariantCulture.NumberFormat;
m_TestCulture.DateTimeFormat = CultureInfo.InvariantCulture.DateTimeFormat;
CultureInfo.CurrentCulture = m_TestCulture;
}
protected vitrual void Dispose(bool disposing) {
if (disposing) {
if (!m_IsDisposed && ReferenceEquals(CultureInfo.CurrentCulture, m_TestCulture)) {
CultureInfo.CurrentCulture = m_SavedCulture;
m_IsDisposed = true;
}
}
}
public void Dispose() => Dispose(true);
}
然后按如下方式使用它:
using (new TestCulture()) {
// Tests which should be run under the specific culture
}
关于c# - 如何将DateTimeFormatInfo.CurrentInfo和NumberFormatInfo.CurrentInfo设置为不变式?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/55371994/