簡體   English   中英

將枚舉傳遞給用作枚舉和類型的方法

[英]Pass enum to method to be used as enum and type

我正在嘗試將枚舉傳遞到將為gridview創建列的方法中。 我可以將Enum作為Enum passEnum或Type 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()方法獲取附加到枚舉的字符串值。 它要求將一個枚舉傳遞給它。 如何使它在某種方法中起作用?

似乎您要在此處編寫泛型方法,而無需實際使用泛型,請嘗試如下操作:

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的代碼,因此我自己沒有對此進行編譯,但是我認為應該沒問題。 讓我知道是否仍然存在問題。

我通過為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++;
            }
        }

我更喜歡使用泛型,正如Mike所發布的...但是該行仍然存在問題:

bf.HeaderText = StringEnum.GetString((TEnum)c);

它調用的方法需要一個用Mike的代碼編寫的Enum和“ 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;
        }

如果有人知道像Mike那樣的方法(使用泛型),我可以做到這一點,我將不勝感激。 現在,這完全是學習和知識的問題,因為我已經實現了上面提供的解決方案,但是我仍然想知道如何以一種真正通用的方式實現此目標……謝謝!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM