Enum helper in c# not giving expected result

℡╲_俬逩灬. 提交于 2021-02-16 20:18:22

问题


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


回答1:


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.



来源:https://stackoverflow.com/questions/5678612/enum-helper-in-c-sharp-not-giving-expected-result

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!