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

发布于 2024-10-30 18:09:05 字数 1252 浏览 4 评论 0原文

我正在尝试将枚举传递到将为网格视图创建列的方法中。我可以将 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() 方法获取附加到枚举的字符串值。它需要将枚举传递给它。我怎样才能让它在某种方法中发挥作用?

I'm trying to pass an Enum into a method that will create columns for a gridview. I can pass the Enum as Enum passEnum OR Type enumType, and either works, just not together. What I mean is if I pass it as a type the Enum.GetNames() method accepts it, and if I pass it as an enum, the StringEnum.GetString() method accepts it. But I can't pass one and have them both accept it and I can't pass them separately (enum and type) and have both accept it. The method that AMOST works:

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++;
    }
}

I get a red squiggle line under passEnum that says: "The type or namespace 'passEnum' cannot be found... etc". For some reason I can get this to work outside of a method like this:

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++;
}

The StringEnum.GetString() method gets a string value attached to the enum. It requires an enum to be passed to it. How can I get this to work in a method?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

半葬歌 2024-11-06 18:09:05

我通过为 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);

它调用的方法需要一个枚举,并且迈克代码中编写的“enumType”显然不被视为枚举,因为我收到错误“无法从 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 的方法(即使用泛型),我可以完成这个任务,我会很感激,现在这都是学习和知识的问题,因为我已经实现了上面的解决方案,但我仍然想知道如何做到这一点。一种真正通用的方式...谢谢!

I worked around the problem by writing a new method for the StringEnum class that returns a list of string values for the enum instead of trying to pull each string indivually... like this:

       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++;
            }
        }

I would prefer using the generics, as Mike has posted... but there is still an issue with the line:

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

The method that it calls needs an Enum and "enumType" as written in Mike's code is not considered an Enum apparently because I get an error "cannot convert from TEnum to System.Enum, here is the method it calls:

        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;
        }

I did not write the method above (or the StringEnum class)... but here is the method that I added to get the list of enum strings:

        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;
        }

If anyone knows a way that is like Mike's (that uses generics) that I can get this done I'd appreciate it. It is all a matter of learning and knowledge now because I've already implemented the solution that I have above, but I would still like to know how to do this in a truly generic way... thanks!

和我恋爱吧 2024-11-06 18:09:05

看起来您正在尝试在那里编写一个泛型方法,而不实际使用泛型,请尝试这样的事情:

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 的代码,所以我没有自己编译它,但我认为应该没问题。如果仍然有问题请告诉我。

It looks like you're trying to write a generic method there, without actually using generics, try something like this:

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++;
    }
}

It's not really how I'd go about doing it from scratch, but it should work.

edit: Sorry, I forgot to show an example of calling that, which would look like this:

AddColumnsToGridView<MyEnumType>(gridView);

edit 2: I mentioned in the comment below that you'll have problems if the enum doesn't start at 0 or misses out values. You might want to try this instead:

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);
    }
}

Note that this is no longer a generic method, as it doesn't need to be (It's better this way - you won't get multiple versions of the method JITted at runtime for each enum type). Just call it like this:

AddColumnsToGridView(gridView, typeof(MyEnum));

I obviously don't have the code that you have for StringEnum, so I haven't compiled this up myself, but I think it should be fine. Let me know if it's still a problem.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文