创建将T限制为枚举的通用方法(Create Generic method constraining T to an Enum)

编程入门 行业动态 更新时间:2024-10-27 04:26:21
创建将T限制为枚举的通用方法(Create Generic method constraining T to an Enum)

我正在构建一个扩展Enum.Parse概念的函数

允许在未找到枚举值的情况下解析默认值 不区分大小写

所以我写了以下内容:

public static T GetEnumFromString<T>(string value, T defaultValue) where T : Enum { if (string.IsNullOrEmpty(value)) return defaultValue; foreach (T item in Enum.GetValues(typeof(T))) { if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return item; } return defaultValue; }

我得到一个错误约束不能是特殊的类'System.Enum'。

但是还有一个解决方案是允许Generic Enum,或者我将不得不模仿Parse功能,并将一个类型作为一个属性传递给你的代码。

谢谢,谢谢。

已经解决了(我已经离开了循环,以保持不区分大小写的情况 - 解析XML时我正在使用)

public static class EnumUtils { public static T ParseEnum<T>(string value, T defaultValue) where T : struct, IConvertible { if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type"); if (string.IsNullOrEmpty(value)) return defaultValue; foreach (T item in Enum.GetValues(typeof(T))) { if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return item; } return defaultValue; } }

编辑: (2015年2月16日)Julien Lebosquain最近在下面发布了一个编译器强制解决方案 ,这是值得一看的,并且是一个upvote。 如果解决方案向上浮动页面,我将删除此编辑。

I'm building a function to extend the Enum.Parse concept that

Allows a default value to be parsed in case that an Enum value is not found Is case insensitive

So I wrote the following:

public static T GetEnumFromString<T>(string value, T defaultValue) where T : Enum { if (string.IsNullOrEmpty(value)) return defaultValue; foreach (T item in Enum.GetValues(typeof(T))) { if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return item; } return defaultValue; }

I am getting a Error Constraint cannot be special class System.Enum.

Fair enough, but is there a workaround to allow a Generic Enum, or am I going to have to mimic the Parse function and pass a type as an attribute, which forces the ugly boxing requirement to your code.

EDIT All suggestions below have been greatly appreciated, thanks.

Have settled on (I've left the loop to maintain case insensitivity - I am using this when parsing XML)

public static class EnumUtils { public static T ParseEnum<T>(string value, T defaultValue) where T : struct, IConvertible { if (!typeof(T).IsEnum) throw new ArgumentException("T must be an enumerated type"); if (string.IsNullOrEmpty(value)) return defaultValue; foreach (T item in Enum.GetValues(typeof(T))) { if (item.ToString().ToLower().Equals(value.Trim().ToLower())) return item; } return defaultValue; } }

EDIT: (16th Feb 2015) Julien Lebosquain has recently posted a compiler enforced type-safe generic solution in MSIL or F# below, which is well worth a look, and an upvote. I will remove this edit if the solution bubbles further up the page.

最满意答案

由于Enum类型实现IConvertible接口,更好的实现应该是这样的:

public T GetEnumFromString<T>(string value) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new ArgumentException("T must be an enumerated type"); } //... }

这仍将允许通过实施IConvertible的价值类型。 机会很少见。

Since Enum Type implements IConvertible interface, a better implementation should be something like this:

public T GetEnumFromString<T>(string value) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new ArgumentException("T must be an enumerated type"); } //... }

This will still permit passing of value types implementing IConvertible. The chances are rare though.

更多推荐

本文发布于:2023-07-29 20:16:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1319450.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法   Create   Generic   Enum   constraining

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!