Enum in C# and foreach

Possible Duplicate:
C#: How to enumerate an enum?

Hi All,

I have an Enum

public enum AttributeType
    {
        TextField = 1, 
        TextArea = 2,
        Date = 4, 
        Boolean = 8
    }

I want to foreach this enum and make an object array of it in this format

object data = new object[]
{
   // new object[] { 1,"TextField"}
   new object[] { enumValue, enumText}
};

Well, this would do it (assuming .NET 3.5):

var allValues = (AttributeType[]) Enum.GetValues(typeof(AttributeType));

var array = allValues.Select(value => new object[] { value, value.ToString() })
                     .ToArray();

or use an anonymous type:

var array = allValues.Select(value => { Value = value, Name = value.ToString() })
                     .ToArray();
链接地址: http://www.djcxy.com/p/16288.html

上一篇: 循环访问C#枚举的键和值

下一篇: 枚举C#和foreach