如何传递枚举作为参数?
在下面的代码中,我只是将字符串(例如“medium”)转换为其枚举值。我需要做的不是将 Opacity 作为固定的 Enum 类型,而是将其作为参数传递,以便该函数可以对任何 Enum 进行操作。这似乎比我预期的更困难,即“Enum MyEnum”不起作用。有人解决吗?
public enum Opacity
{
Low,
Medium,
High
}
public static Enum StringToEnum(String str)
{
return (Opacity)Enum.Parse(typeof(Opacity), str, true); // Case insensitive
}
In the code below I am just coverting a string ("medium" for example) to its Enum value. What I need to be able to do is rather than having Opacity as a fixed Enum type, pass that in as an argument as well so that the function operates on any Enum. This seems to be proving more difficult than I anticipated, i.e. 'Enum MyEnum' doesn't work. Solutions anyone?
public enum Opacity
{
Low,
Medium,
High
}
public static Enum StringToEnum(String str)
{
return (Opacity)Enum.Parse(typeof(Opacity), str, true); // Case insensitive
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)