在泛型类中定义默认值枚举时,我有一些奇怪的行为。MS .NET 4.0 使用默认值,但 mono 似乎回退到第一个枚举定义(即零值)。
我写了一个小测试,它只能在 MS .NET 上正确运行,但不能在单声道上正确运行。
using System;
using NUnit.Framework;
namespace enumErrorTest
{
class EnumWrapperCtor<T> {
public enum Test {
Wrong,
MyDefault
}
private readonly T otherVal;
private readonly Test myVal;
public EnumWrapperCtor (Test value = Test.MyDefault)
{
myVal = value;
}
public Test getValue ()
{
return myVal;
}
public Test echo (Test value = Test.MyDefault)
{
return value;
}
}
class EnumWrapperField<T> {
public enum Test {
Wrong,
MyDefault
}
private readonly T otherVal;
private readonly Test myVal = Test.MyDefault;
public Test getValue ()
{
return myVal;
}
}
class EnumWrapper {
public enum Test {
Wrong,
MyDefault
}
private readonly Test myVal;//= Test.MyDefault;
public EnumWrapper (Test value = Test.MyDefault)
{
myVal = value;
}
public Test getValue ()
{
return myVal;
}
}
[TestFixture]
class Proof
{
[Test]
public void NoGenerics ()
{
var ew = new EnumWrapper();
Assert.AreEqual(EnumWrapper.Test.MyDefault, ew.getValue());
}
[Test]
public void Ctor ()
{
var ew = new EnumWrapperCtor<int>();
Assert.AreEqual(EnumWrapperCtor<int>.Test.MyDefault, ew.getValue());
}
[Test]
public void Field ()
{
var ew = new EnumWrapperField<int>();
Assert.AreEqual(EnumWrapperField<int>.Test.MyDefault, ew.getValue());
}
[Test]
public void Echo ()
{
var ew = new EnumWrapperCtor<int>();
Assert.AreEqual(EnumWrapperCtor<int>.Test.MyDefault, ew.echo());
}
}
}
失败的测试是Ctor
和Echo
。
# nunit-console bin/Debug/enumErrorTest.exe
NUnit-Console version 2.6.0.0
Copyright (C) 2002-2012 Charlie Poole.
Copyright (C) 2002-2004 James W. Newkirk, Michael C. Two, Alexei A. Vorontsov.
Copyright (C) 2000-2002 Philip Craig.
All Rights Reserved.
Runtime Environment -
OS Version: Unix 3.2.0.35
CLR Version: 4.0.30319.1 ( Mono 4.0 ( 2.10.8.1 (Debian 2.10.8.1-5~dhx1~precise1) ) )
ProcessModel: Default DomainUsage: Single
Execution Runtime: mono-4.0
.F.F..
Tests run: 4, Errors: 0, Failures: 2, Inconclusive: 0, Time: 0.070971 seconds
Not run: 0, Invalid: 0, Ignored: 0, Skipped: 0
Errors and Failures:
1) Test Failure : enumErrorTest.Proof.Ctor
Expected: MyDefault
But was: Wrong
at enumErrorTest.Proof.Ctor () [0x00000] in <filename unknown>:0
2) Test Failure : enumErrorTest.Proof.Echo
Expected: MyDefault
But was: Wrong
at enumErrorTest.Proof.Echo () [0x00000] in <filename unknown>:0