通用约束中的枚举

发布于 2024-11-30 04:57:32 字数 579 浏览 1 评论 0原文

我为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

人生戏 2024-12-07 04:57:32

是的,有一个解决方法。但你可能不喜欢它。您必须重写 IL 来表达您想要的约束 - 因为 CLR 允许,但 C# 不允许。 (编译器尊重约束;它只是不允许您用 C# 代码表达它。)

我有一个名为 Unconstrained Melody 正是这样做的,在 博客文章

遗憾的是您无法表达这一点,也许它会在该语言的未来版本中得到修复。目前,据我所知,IL 重写就是全部。

编辑:我刚刚在 VB 中尝试了您想要的约束:

Foo(Of T As { System.Enum, Structure }) (...)

编译器抱怨:

error BC32061: 'Enum' cannot be used as a type constraint.

所以不,您也不能在 VB 中执行此操作。奇怪的是,有关该错误的网页没有提及该限制...

编辑:对于任何想要使用 Unconstrained Melody 的人来说,需要执行几个步骤才能使其正常工作:

  • 您需要确保您有一个适当的 SDK 目录,如ConstraintChanger\Program.cs。特别是,检查 \Program Files\Microsoft SDKs\Windows 以查看您拥有的版本 - 并适当更改 Program.cs 关键的
  • 是,您需要在顶层有一个名为“Rewriting”的目录(即与 lib 一起)
  • 如果您使用的是 VS2010,您需要在开始时进行项目升级

一旦所有这些都正确,您应该能够点击 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:

Foo(Of T As { System.Enum, Structure }) (...)

And the compiler complains with:

error BC32061: 'Enum' cannot be used as a type constraint.

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:

  • You need to make sure you have an appropriate SDK directory as referred to by ConstraintChanger\Program.cs. In particular, check in \Program Files\Microsoft SDKs\Windows to see what version you've got - and change Program.cs appropriately
  • Critically, you need a directory called "Rewritten" at the top level (i.e. alongside lib)
  • If you're using VS2010 you'll need to go through the project upgrade at the start

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...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文