1

在泛型类中定义默认值枚举时,我有一些奇怪的行为。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());
        }
    }
}

失败的测试是CtorEcho

# 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
4

1 回答 1

0

这实际上是 Mono C# 编译器中的一个错误。您可以通过指定默认参数参数轻松解决它。

Mono 版本 3.0.3 或更高版本应该已修复此问题。

于 2013-01-04T15:03:05.917 回答