C# 中的枚举助手未给出预期结果
基本上,由于某种原因,我没有收到正确的枚举类型,我无法弄清楚为什么,我的代码在下面,非常感谢您的任何指针/解释...
编辑:类型->更改为另一个名称(谢谢大家的提醒)
助手:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
该枚举具有与
TextArea
具有相同基础值的重复成员(Bool
和Choices
)。尽管解析应该成功,但生成的枚举实例上的ToString
值未定义,并且可能不等于您的断言所期望的“TextArea”。从
Enum.ToString
文档:编辑:
为了响应您的编辑,请尝试以下断言:
或者如果您更喜欢比较基础类型:
您似乎认为枚举成员不与基础值关联,如果其值为未明确指定。事实并非如此;枚举的所有成员都与底层值相关联。 “隐式”关联的规则由(来自 语言规范):
The enumeration has duplicate members with the same underlying value as
TextArea
(Bool
andChoices
). Although the parse should succeed, the value ofToString
on the resulting enum instance is not defined, and may not equal "TextArea" as your assertion is expecting.From the
Enum.ToString
documentation:EDIT:
In response to your edit, try this assertion:
or if you prefer comparing the underlying type:
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):