枚举的扩展方法,而不是枚举的实例
我的事物有一个枚举,如下所示:
public enum Things
{
OneThing,
AnotherThing
}
我想为此枚举编写一个扩展方法(类似于 Prise 的答案在这里),但是虽然该方法适用于枚举实例,阿拉
Things thing; var list = thing.ToSelectList();
我希望它能够在实际枚举上工作:
var list = Things.ToSelectList();
我可以这样做,
var list = default(Things).ToSelectList();
但我不喜欢它的外观:)
我已经通过以下扩展方法更接近了:
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;
}
}
像这样使用:
var list = typeof(Things).ToSelectList();
可以我们还能做得更好吗?
I have an enumeration for my Things like so:
public enum Things
{
OneThing,
AnotherThing
}
I would like to write an extension method for this enumeration (similar to Prise's answer here) but while that method works on an instance of the enumeration, ala
Things thing; var list = thing.ToSelectList();
I would like it to work on the actual enumeration instead:
var list = Things.ToSelectList();
I could just do
var list = default(Things).ToSelectList();
But I don't like the look of that :)
I have gotten closer with the following extension method:
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;
}
}
Used like so:
var list = typeof(Things).ToSelectList();
Can we do any better than that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
扩展方法仅适用于实例,因此无法完成,但是通过一些精心选择的类/方法名称和泛型,您可以产生看起来同样好的结果:
然后您可以获得这样的选择列表:
IMO这比
Things.ToSelectList()
读起来要好得多。Extension methods only work on instances, so it can't be done, but with some well-chosen class/method names and generics, you can produce a result that looks just as good:
Then you can get your select list like this:
IMO this reads a lot better than
Things.ToSelectList()
.不。
你能做的最好的就是将它放在静态类中,如下所示:
No.
The best you can do is put it on a static class, like this:
Aaronaught的答案真的很棒,基于此我做了以下实现:
在我看来,它更安全一点,因为你可以 -几乎 - 仅使用枚举来调用它,当然,如果您想要一个无异常版本,您可以简单地返回 null 而不是抛出。
Aaronaught's answer is really great, based on that I made the following implementation:
In my opinion it's a little bit safer, as you can - almost - call it only with Enums, and of course instead of the throw you can simply return null if you want an exception-free version.
我使用“Type”而不是“Enum”来添加扩展。然后我可以从该方法中获取任何类型的列表。这里它返回字符串值:
稍后我可以使用此语法来获取我想要的内容:
I use 'Type' instead of 'Enum' to add extension. Then I can get any type of list back from the method. Here it returns string values:
Later I could use this syntax to get what I want:
@Aaronaught 有一个很好的答案。为了扩展他的答案,你甚至可以使其更通用。我在全局库中有这个...
现在您已将此功能与 SelectList 类分离。因此,您可以在 SelectList 方法中或其他任何地方调用它。
@Aaronaught has a very good answer. To extend his answer, you can also even make it more generic. I have this in a global library...
Now you have de-coupled this functionality from the SelectList class. So you can call this in your SelectList methods, or anywhere else for that matter.
在我看来,这是最干净的方式。为什么?
new
,这是一个小小的权衡(因为它必须有一个实例才能工作。null
,它实际上是如果您尝试将其与其他类型一起使用,则无法编译。用法:
扩展方法:
In my opinion, this is the cleanest way. Why?
System.Enum
new
and that's a small trade off (because it has to have an instance in order to work.null
around and it literally won't compile if you try to use it with another type.Usage:
Extension Method:
我认为,最接近的方法就是对事物进行一些虚拟化,使其像枚举一样工作,而不是枚举。这是我想到的——只是在枚举上放置一个静态方法似乎需要做很多工作,尽管我确实理解它的编程吸引力:
The closest you can come, I think, is to dummy things up a bit to work like an enum without being one. Here's what I've come up with--it seems like a lot of work just to plop a static method on an enumeration, although I do understand the programming appeal of it: