开发者

Enum helper in c# not giving expected result

Basically I am not recieving the correct enum type for some reason and I cannot figure out why, my code is below, many thanks in advance for any pointers/ explanation...

EDIT: type-> changed to anothername (thanks guys for the heads up)

Helper:

 public static T Convert<T>(this string str)
    {
        return (T)Enum.Parse(typeof(T), str, true);
    }
开发者_运维技巧

Enum values:

public enum anothername
    {
        SmallText = 100,
        Number = 15,
        TextArea = 0,
        Bool = 0,
        Choices = 0,
    }

My test:

 [Test]
        public void EnumGetStringFromEnumType()
        {
            //arrange
            var MaxLength = EnumHelper.Convert<anothername>("TextArea").ToString();

            //act

            //assert
            Assert.AreEqual("TextArea", MaxLength);


        }

EDIT:

Thanks, removing the int values solved it!

However... what if I actually wanted to have say values for some enum types and not other e.g.

public enum anothername
    {
        SmallText = 100,
        Number = 15,
        TextArea,
        Bool,
        Choices,
    }

Test 2:

[Test]
        public void EnumGetIntValueOrStringFromEnumType()
        {
            //arrange
            var MaxLength = EnumHelper.ToEnumSafe<anothername>("TextArea");

            //act

            //assert
            Assert.AreEqual(null, (int)MaxLength);

        }

I have exactly the same problem when I try and retrieve the int values, I get incorrect results... result = 16


The enumeration has duplicate members with the same underlying value as TextArea (Bool and Choices). Although the parse should succeed, the value of ToString on the resulting enum instance is not defined, and may not equal "TextArea" as your assertion is expecting.

From the Enum.ToString documentation:

If multiple enumeration members have the same underlying value and you attempt to retrieve the string representation of an enumeration member's name based on its underlying value, your code should not make any assumptions about which name the method will return.

EDIT:

In response to your edit, try this assertion:

var MaxLength = EnumHelper.ToEnumSafe<anothername>("TextArea");
Assert.AreEqual(anotherName.TextArea, MaxLength);

or if you prefer comparing the underlying type:

Assert.AreEqual((int)anotherName.TextArea, (int)MaxLength);

You appear to be under the impression that an enum member is not associated with an underlying value if its value is not explicitly specified. This is not the case; all members of an enum are associated with an underlying value. The rules for the 'implicit' associations are given by (from the language specification):

• If the enum member is the first enum member declared in the enum type, its associated value is zero.

• Otherwise, the associated value of the enum member is obtained by increasing the associated value of the textually preceding enum member by one. This increased value must be within the range of values that can be represented by the underlying type, otherwise a compile-time error occurs.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜