C# 中的枚举助手未给出预期结果

发布于 2024-11-01 05:22:09 字数 1372 浏览 0 评论 0原文

基本上,由于某种原因,我没有收到正确的枚举类型,我无法弄清楚为什么,我的代码在下面,非常感谢您的任何指针/解释...

编辑:类型->更改为另一个名称(谢谢大家的提醒)

助手:

 public static T Convert<T>(this string str)
    {
        return (T)Enum.Parse(typeof(T), str, true);
    }

枚举值:

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

我的测试:

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

            //act

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


        }

编辑:

谢谢,删除 int 值解决了它!

但是...如果我实际上想为某些枚举类型而不是其他类型提供值,例如

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

测试 2:

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

            //act

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

        }

当我尝试检索 int 值时,我遇到了完全相同的问题,我得到了不正确的结果... 结果 = 16

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2024-11-08 05:22:09

该枚举具有与 TextArea 具有相同基础值的重复成员(BoolChoices)。尽管解析应该成功,但生成的枚举实例上的 ToString 值未定义,并且可能不等于您的断言所期望的“TextArea”。


Enum.ToString 文档:

如果多个枚举成员有
和你有相同的潜在价值
尝试检索字符串
枚举的表示
基于其底层的成员名称
值,您的代码不应产生任何
关于哪个名字的假设
方法将返回。

编辑

为了响应您的编辑,请尝试以下断言:

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

或者如果您更喜欢比较基础类型:

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

您似乎认为枚举成员不与基础值关联,如果其值为未明确指定。事实并非如此;枚举的所有成员都与底层值相关联。 “隐式”关联的规则由(来自 语言规范):

• 如果枚举成员是第一个枚举
枚举类型中声明的成员,其
相关值为零。

• 否则,
枚举的关联值
成员是通过增加
文本的关联值
在枚举成员前面加一位。这
增加的价值必须在
的值范围可以是
由底层类型表示,
否则会出现编译时错误。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文