Jam*_*zor 44 c# extension-methods enums
我对我的事情进行了枚举,如下:
public enum Things
{
OneThing,
AnotherThing
}
Run Code Online (Sandbox Code Playgroud)
我想为这个枚举编写一个扩展方法(类似于Prize的答案)但是该方法适用于枚举的实例,ala
Things thing; var list = thing.ToSelectList();
Run Code Online (Sandbox Code Playgroud)
我希望它能用于实际的枚举:
var list = Things.ToSelectList();
Run Code Online (Sandbox Code Playgroud)
我可以这样做
var list = default(Things).ToSelectList();
Run Code Online (Sandbox Code Playgroud)
但我不喜欢那样:)
我已经接近以下扩展方法:
public static SelectList ToSelectList(this Type type)
{
if (type.IsEnum)
{
var values = from Enum e in Enum.GetValues(type)
select new { ID = e, Name = e.ToString() };
return new SelectList(values, "Id", "Name");
}
else
{
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
像这样使用:
var list = typeof(Things).ToSelectList();
Run Code Online (Sandbox Code Playgroud)
我们可以做得更好吗?
Aar*_*ght 72
扩展方法仅适用于实例,因此无法完成,但通过一些精心选择的类/方法名称和泛型,您可以生成看起来同样好的结果:
public class SelectList
{
// Normal SelectList properties/methods go here
public static SelectList Of<T>()
{
Type t = typeof(T);
if (t.IsEnum)
{
var values = from Enum e in Enum.GetValues(type)
select new { ID = e, Name = e.ToString() };
return new SelectList(values, "Id", "Name");
}
return null;
}
}
Run Code Online (Sandbox Code Playgroud)
然后你可以得到这样的选择列表:
var list = SelectList.Of<Things>();
Run Code Online (Sandbox Code Playgroud)
国际海事组织比这读得好多了Things.ToSelectList().
没有.
你能做的最好的事情是把它放在静态类上,如下所示:
public static class ThingsUtils {
public static SelectList ToSelectList() { ... }
}
Run Code Online (Sandbox Code Playgroud)
Aaronaught 的回答真的很棒,基于此我做了以下实现:
public class SelectList
{
public static IEnumerable<Enum> Of<T>() where T : struct, IConvertible
{
Type t = typeof(T);
if (t.IsEnum)
{
return Enum.GetValues(t).Cast<Enum>();
}
throw new ArgumentException("<T> must be an enumerated type.");
}
}
Run Code Online (Sandbox Code Playgroud)
在我看来,它更安全一些,因为您几乎可以仅使用枚举来调用它,当然,如果您想要一个无异常版本,您可以简单地返回 null 而不是抛出。
| 归档时间: |
|
| 查看次数: |
27362 次 |
| 最近记录: |