[英]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.