将枚举传递给用作枚举和类型的方法

我正在尝试将Enum传递给一个方法,该方法将为gridview创建列。我可以将枚举作为Enum passEnum或类型enumType传递,并且可以正常工作,但不能一起使用。我的意思是如果我将它作为一个类型传递,Enum.GetNames()方法接受它,如果我将它作为枚举传递,StringEnum.GetString()方法接受它。但我不能通过一个让他们都接受它而我不能单独传递它们(枚举和类型)并且都接受它。 AMOST工作的方法:
public static void AddColumnsToGridView(GridView gv, Enum passEnum, Type enumType)
{
    BoundField bf = new BoundField();
    int c = 0;
    foreach (string item in Enum.GetNames(enumType))
    {
        bf = new BoundField();
        bf.HeaderText = StringEnum.GetString((passEnum)c);
        bf.DataField = item;
        bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
        bf.SortExpression = item;
        gv.Columns.Add(bf);
        c++;
    }
}
我在passEnum下面得到一条红色的波浪线,上面写着:“无法找到类型或命名空间'passEnum'等等。”出于某种原因,我可以在这样的方法之外使用它:
BoundField bf = new BoundField();
int c = 0;
foreach (string item in Enum.GetNames(typeof(PatientRX)))
{
    bf = new BoundField();
    bf.HeaderText = StringEnum.GetString((PatientRX)c);
    bf.DataField = item;
    bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
    bf.SortExpression = item;
    gvRX.Columns.Add(bf);
    c++;
}
StringEnum.GetString()方法获取附加到枚举的字符串值。它需要一个枚举传递给它。如何才能在方法中使用它?     
已邀请:
我通过为StringEnum类编写一个新方法来解决这个问题,该方法返回枚举的字符串值列表,而不是试图单独拉出每个字符串...像这样:
       public static void AddColumnsToGridView(GridView gv, Type enumType)
       {
           gv.Columns.Clear();
           List<string> headers = StringEnum.GetStringValueList(enumType);
           BoundField bf = new BoundField();
           int c = 0;
           foreach (string item in Enum.GetNames(enumType))
           {
              bf = new BoundField();
              bf.HeaderText = headers[c];
              bf.DataField = item;
              bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
              bf.SortExpression = item;
              gv.Columns.Add(bf);
              c++;
            }
        }
我更喜欢使用仿制药,就像迈克发布的那样......但是这条线仍然存在问题:
bf.HeaderText = StringEnum.GetString((TEnum)c);
它调用的方法需要一个枚举,并且Mike的代码中写的“enumType”不被视为Enum,显然是因为我收到错误“无法从TEnum转换为System.Enum,这里是它调用的方法:
        public static string GetString(Enum value)
        {
            string output = null;
            Type type = value.GetType();

            if (_stringValues.ContainsKey(value))
                output = (_stringValues[value] as StringValueAttribute).Value;
            else
            {
                //Look for our 'StringValueAttribute' in the field's custom attributes
                FieldInfo fi = type.GetField(value.ToString());
                StringValueAttribute[] attrs = fi.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
                if (attrs.Length > 0)
                {
                    _stringValues.Add(value, attrs[0]);
                    output = attrs[0].Value;
                }
            }
            return output;
        }
我没有编写上面的方法(或StringEnum类)...但这里是我为获取枚举字符串列表而添加的方法:
        public static List<string> GetStringValueList(Type enumType)
        {
            List<string> values = new List<string>();
            //Look for our string value associated with fields in this enum
            foreach (FieldInfo fi in enumType.GetFields())
            {
                //Check for our custom attribute
                var stringValueAttributes = fi.GetCustomAttributes(typeof(StringValueAttribute), false) as StringValueAttribute[];
                if (stringValueAttributes.Length > 0)
                {
                    values.Add(stringValueAttributes[0].Value);
                }
            }
            return values;
        }
如果有人知道迈克的方式(使用泛型)我可以完成这件事,我会很感激。这完全是学习和知识的问题,因为我已经实现了上面的解决方案,但我仍然想知道如何以一种真正通用的方式做到这一点......谢谢!     
看起来你正试图在那里编写泛型方法,而不是实际使用泛型,尝试这样的事情:
public static void AddColumnsToGridView<TEnum>(GridView gv)
{
    Type enumType = typeof(TEnum);
    BoundField bf = new BoundField();
    int c = 0;
    foreach (string item in Enum.GetNames(enumType))
    {
        bf = new BoundField();
        bf.HeaderText = StringEnum.GetString((Enum)c);
        bf.DataField = item;
        bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
        bf.SortExpression = item;
        gv.Columns.Add(bf);
        c++;
    }
}
这不是我从头开始做的事情,但它应该有效。 编辑:对不起,我忘了显示一个调用它的示例,它看起来像这样:
AddColumnsToGridView<MyEnumType>(gridView);
编辑2:我在下面的评论中提到,如果枚举不是从0开始或者错过了值,你就会遇到问题。你可能想尝试这样做:
public static void AddColumnsToGridView(GridView gv, Type enumType)
{
    Array values = Enum.GetValues(enumType)
    string[] names= Enum.GetNames(enumType)

    BoundField bf = new BoundField();
    for (int i = 0; i < names.Length; i++)
    {
        bf = new BoundField();
        bf.HeaderText = StringEnum.GetString((Enum)values.GetValue(i));
        bf.DataField = names[i];
        bf.ItemStyle.CssClass = "siteFont leftPaddingThree";
        bf.SortExpression = names[i];
        gv.Columns.Add(bf);
    }
}
请注意,这不再是通用方法,因为它不需要(这样更好 - 您不会在运行时为每个枚举类型获得JITted的多个版本)。只需将其称为:
AddColumnsToGridView(gridView, typeof(MyEnum));
我显然没有StringEnum的代码,所以我没有自己编译,但我认为应该没问题。如果它仍然是个问题,请告诉我。     

要回复问题请先登录注册