将枚举传递给用作枚举和类型的方法
我正在尝试将枚举传递到将为网格视图创建列的方法中。我可以将 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我通过为 StringEnum 类编写一个新方法来解决这个问题,该方法返回枚举的字符串值列表,而不是尝试单独提取每个字符串......如下所示:
我更喜欢使用泛型,正如 Mike 所发布的那样。 ..但该行仍然存在问题:
它调用的方法需要一个枚举,并且迈克代码中编写的“enumType”显然不被视为枚举,因为我收到错误“无法从 TEnum 转换为 System.Enum,这是它调用的方法:
我没有编写上面的方法(或 StringEnum 类)...但这是我添加的用于获取枚举字符串列表的方法:
如果有人知道类似于 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:
I would prefer using the generics, as Mike has posted... but there is still an issue with the line:
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:
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:
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!
看起来您正在尝试在那里编写一个泛型方法,而不实际使用泛型,请尝试这样的事情:
这并不是我从头开始做的方式,但它应该可以工作。
编辑:抱歉,我忘记展示一个调用它的示例,如下所示:
编辑2:我在下面的评论中提到,如果枚举不从 0 开始或错过,您将会遇到问题价值观。您可能想尝试这个:
请注意,这不再是通用方法,因为它不需要(这样更好 - 您不会在运行时获得该方法的多个版本 JITted)每个枚举类型)。就像这样调用它:
我显然没有 StringEnum 的代码,所以我没有自己编译它,但我认为应该没问题。如果仍然有问题请告诉我。
It looks like you're trying to write a generic method there, without actually using generics, try something like this:
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:
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:
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:
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.