通用约束中的枚举
我为 Enums (Enumerations) 构建了一个扩展方法 - 命名为 GetEnumSecondName
static string GetEnumSecondName(this Enum myEnumInstance) {...}
现在,我有一个通用方法,它应该采用 Enumeration 并返回该类型的所有第二个名称。
List<string> GetSecondNames<T : ?T:Enum ? >()
{
// ...
foreach T member in GetAllMembers<T>()
// should work only for Enum instances
resultList.Add(member.GetEnumSecondName());
// ...
}
有解决方法吗?
编辑:
据我了解(感谢 Jon Skeet),C# 不支持 Thins 类型的约束。如果有VB.NET专家确认“普通”VB.NET也不支持它。谢谢。
I built a extension method for Enums (Enumerations) - , name it, say GetEnumSecondName
static string GetEnumSecondName(this Enum myEnumInstance) {...}
Now, I have a generic method, that should take a Enumeration and return all the second names for that type.
List<string> GetSecondNames<T : ?T:Enum ? >()
{
// ...
foreach T member in GetAllMembers<T>()
// should work only for Enum instances
resultList.Add(member.GetEnumSecondName());
// ...
}
Is there a workaround to do it?
Edit:
As I understood (thanks to Jon Skeet), C# does not support thins kind of constraint. If there are any VB.NET expert to confirm that "ordinary" VB.NET does not support it either. Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
是的,有一个解决方法。但你可能不喜欢它。您必须重写 IL 来表达您想要的约束 - 因为 CLR 允许,但 C# 不允许。 (编译器尊重约束;它只是不允许您用 C# 代码表达它。)
我有一个名为 Unconstrained Melody 正是这样做的,在 博客文章。
遗憾的是您无法表达这一点,也许它会在该语言的未来版本中得到修复。目前,据我所知,IL 重写就是全部。
编辑:我刚刚在 VB 中尝试了您想要的约束:
编译器抱怨:
所以不,您也不能在 VB 中执行此操作。奇怪的是,有关该错误的网页没有提及该限制...
编辑:对于任何想要使用 Unconstrained Melody 的人来说,需要执行几个步骤才能使其正常工作:
ConstraintChanger\Program.cs
。特别是,检查\Program Files\Microsoft SDKs\Windows
以查看您拥有的版本 - 并适当更改 Program.cs 关键的lib
一起)一旦所有这些都正确,您应该能够点击 Ctrl-Shift-B 并得到一个工作版本。 不要删除和替换项目引用 - 测试程序集需要引用重写的程序集,而不是创建它的项目。
今晚我将尝试解决其中一些问题 - 甚至可能创建一个 Nuget 包......
Yes, there's a workaround. You may not like it though. You have to rewrite the IL to express the constraint you want - because the CLR allows it, but C# doesn't. (The compiler respects the constraint; it just doesn't let you express it in C# code.)
I have a project called Unconstrained Melody which does exactly this, introduced in a blog post.
It's regrettable that you can't express this, and maybe it'll be fixed in a future version of the language. For now, IL rewriting is all there is as far as I know.
EDIT: I've just tried the constraint you'd want in VB:
And the compiler complains with:
So no, you can't do it in VB either. Oddly enough, the web page about that error doesn't mention the restriction...
EDIT: To anyone wanting to play with Unconstrained Melody, there are a few steps required to get it working:
ConstraintChanger\Program.cs
. In particular, check in\Program Files\Microsoft SDKs\Windows
to see what version you've got - and change Program.cs appropriatelylib
)Once all of that is correct, you should just be able to hit Ctrl-Shift-B and get a working build. Do not remove and replace the project references - the test assembly needs to refer to the rewritten one, not the project it's created from.
I'll attempt to address some of these issues tonight - and possibly even create a Nuget package...