如何在foreach循环中将字符串转换为变量Name(Enum),以便迭代它们? [英] How can I convert a string to a variable Name(Enum) in a foreach loop, so that it would iterate over them?
问题描述
enum abcd {read}
enum read {view,edit}
method
{
abcd opname =(abcd) 1 ;
}
foreach ( var i in Enum.GetValues(typeOf(opname)))
{
........
}
It在 opname
中显示错误。
我无法做到。
所以请给我一个建议。
foreach (< span class =code-keyword> var i Enum.GetValues( typeof ( abcd ))){}
typeof(T)
其中T
是枚举类型名称
,而不是枚举对象值 ...
开始编辑#1 ...
在另一个枚举中包含Enum元素并访问的示例包含枚举的内容。// 使用这两个枚举:
public enum someEnum
{
一,二,三,四,五
}
public enum someOtherEnum
{
someEnum,six,seven,8
}
// 在某个方法中......
// 注意所需的演员!
var enu =(someEnum)someOtherEnum.someEnum;
foreach ( var v in Enum.GetValues(enu.GetType()))
{
Console.WriteLine(v + value = + Convert.ToInt32(v).ToString());
}
请注意,我认为使用这种类型的代码是错误的,使用带有常量的结构或静态类是一个更好的主意。
结束编辑#1 ...
您的代码向我推荐你不清楚枚举是什么,但你可以像这样迭代枚举中的值:// 使用此枚举
public enum someEnum
{
one,
two,
three,
four,
$ b}
// 在某种方法中......
// 在此处投射到数组可为每个元素保存装箱和拆箱
foreach ( var v in Enum.GetValues( typeof (someEnum)) as someEnum [])
{
Console.WriteLine(v + value = +转换。 ToInt32(v)的ToString());
}您可以迭代Enum常量的名称,如下所示:
foreach (< span class =code-keyword> var v Enum.GetNames( typeof ( someEnum)))
{
Console.WriteLine(v + value = + Convert.ToInt32(Enum.Parse( typeof (someEnum),v))。ToString());
}现在,你的目标是什么;你想做什么?
试试
http://www.tech-recipes.com/rx/21350/how-to-loop-through-enum-values-in-c/ [ ^ ]
http://dotnet.dzone.com / articles / iterate-over-enumeration [ ^
enum abcd{read}
enum read{view,edit}
method
{
abcd opname=(abcd)1;
}
foreach(var i in Enum.GetValues(typeOf(opname)))
{
........
}
It shows an error in opname
.
I'm not able to do.
So please give me a suggestion.
foreach(var i in Enum.GetValues(typeof(abcd))) {}
typeof(T)
whereT
is theEnum Type Name
, not the Enum Object value...
begin edit #1 ...
An example of including an element of an Enum inside another Enum, and accessing the contents of the included Enum.// using these two Enums: public enum someEnum { one, two, three, four, five } public enum someOtherEnum { someEnum, six, seven, eight } // inside some method ... // note the required Cast ! var enu = (someEnum) someOtherEnum.someEnum; foreach (var v in Enum.GetValues(enu.GetType())) { Console.WriteLine(v + " value = " + Convert.ToInt32(v).ToString()); }
Note that I think using this type of code is a mistake, and that using structs, or static classes, with Constants is a better idea.
end edit #1 ...
Your code suggests to me that you are not quite clear on what Enums are, but you can iterate over the values in the Enum like this:// using this Enum public enum someEnum { one, two, three, four, five } // in some method ... // casting to an Array here saves boxing and unboxing for each element foreach(var v in Enum.GetValues(typeof(someEnum)) as someEnum[]) { Console.WriteLine(v + " value = " + Convert.ToInt32(v).ToString()); }You can iterate over the names of the Enum constants like this:
foreach (var v in Enum.GetNames(typeof(someEnum))) { Console.WriteLine(v + " value = " + Convert.ToInt32(Enum.Parse(typeof(someEnum), v)).ToString()); }Now, what is your goal here; what are you trying to do ?
Try
http://www.tech-recipes.com/rx/21350/how-to-loop-through-enum-values-in-c/[^]
http://dotnet.dzone.com/articles/iterate-over-enumeration[^]
这篇关于如何在foreach循环中将字符串转换为变量Name(Enum),以便迭代它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!