[英]Pass enum to method to be used as enum and type
I'm trying to pass an Enum into a method that will create columns for a gridview. 我正在尝试将枚举传递到将为gridview创建列的方法中。 I can pass the Enum as Enum passEnum OR Type enumType, and either works, just not together.
我可以将Enum作为Enum passEnum或Type enumType传递,并且两者都可以,但不能一起使用。 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.
我的意思是,如果我将其作为类型传递,则Enum.GetNames()方法将其接受,如果将其作为枚举传递,则StringEnum.GetString()方法将其接受。 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:
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++;
}
}
I get a red squiggle line under passEnum that says: "The type or namespace 'passEnum' cannot be found... etc". 我在passEnum下得到一条红色的花样线,上面写着:“找不到类型或名称空间'passEnum'...等等”。 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. StringEnum.GetString()方法获取附加到枚举的字符串值。 It requires an enum to be passed to it.
它要求将一个枚举传递给它。 How can I get this to work in a method?
如何使它在某种方法中起作用?
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. 编辑2:我在下面的评论中提到,如果枚举不是从0开始或错过了值,您将遇到问题。 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). 请注意,这不再是通用方法,因为它不是必需的(更好的方法是-您不会在运行时为每种枚举类型获得JITted方法的多个版本)。 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. 我显然没有StringEnum的代码,因此我自己没有对此进行编译,但是我认为应该没问题。 Let me know if it's still a problem.
让我知道是否仍然存在问题。
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: 我通过为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++;
}
}
I would prefer using the generics, as Mike has posted... but there is still an issue with the line: 我更喜欢使用泛型,正如Mike所发布的...但是该行仍然存在问题:
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: 它调用的方法需要一个用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;
}
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: 我没有写上面的方法(或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;
}
If anyone knows a way that is like Mike's (that uses generics) that I can get this done I'd appreciate it. 如果有人知道像Mike那样的方法(使用泛型),我可以做到这一点,我将不胜感激。 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!
现在,这完全是学习和知识的问题,因为我已经实现了上面提供的解决方案,但是我仍然想知道如何以一种真正通用的方式实现此目标……谢谢!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.