C#:将一个枚举包装在另一个枚举中(即镜像另一个枚举/复制它......)

发布于 2024-08-13 05:05:37 字数 629 浏览 3 评论 0原文

这是我的问题:我有一个引用 DLL 的对象。我希望其他对象引用我的对象,而不必包含对 DLL 本身的引用。

这在大多数情况下都很好,除了 DLL 中有一个我想要复制的枚举。我可以逐行写出枚举,但我想知道是否有更好的方法来做到这一点。

IE。

假设 DLL 具有以下枚举:

public enum dllEnum
{
  value1,
  value2,
  value3
}

我可以执行以下操作:

public enum myEnum
{
  value1,
  value2,
  value3
}

或更好:

public enum myEnum
{
  value1 = dllEnum.value1,
  value2 = dllEnum.value2,
  value3 = dllEnum.value3
}

但每种情况都需要我自己写出整个枚举。我宁愿能够将整个枚举包装为我自己的,保留原始枚举的索引。

大致如下:

public enum myEnum
{
  Enum.GetValues(dllEnum)
}

Here's my problem: I have an object that's referencing a DLL. I would like other objects to reference my object, without having to also include a reference to the DLL itself.

This is fine for the most part except there is an enum in the DLL that I would like to replicate. I could write out the enum line by line, but I'm wondering if there's a better way to do this.

ie.

Let's say the DLL's got the following enum:

public enum dllEnum
{
  value1,
  value2,
  value3
}

I could do the following:

public enum myEnum
{
  value1,
  value2,
  value3
}

or better yet:

public enum myEnum
{
  value1 = dllEnum.value1,
  value2 = dllEnum.value2,
  value3 = dllEnum.value3
}

But each of these cases has me writing out the entire enum out myself. I would rather just be able to wrap the entire enum as my own, preserving the indexes of the original enum.

Something along the lines of:

public enum myEnum
{
  Enum.GetValues(dllEnum)
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

2024-08-20 05:05:37

您所问的问题已在此处讨论:

枚举“继承”

What you are asking has been discussed here:

Enum "Inheritance"

困倦 2024-08-20 05:05:37

http://social. msdn.microsoft.com/forums/en-US/netfxbcl/thread/ddedaf59-fb36-45cd-8062-446fa92d8f68

本文提供了一些很好的示例,可以帮助您找到动态生成枚举和在运行时编译它。我已经为 VB.Net 中的“eval”类型函数执行了此操作,但还没有为枚举执行此操作。

这不允许对值进行编译时检查,因此您将在很多方面处于一个受到伤害的世界。借助 .Net 4.0 中的 DLR,我想您会有更大的灵活性(尽管我承认,我还没有阅读该框架下一版本的详细信息。)

http://social.msdn.microsoft.com/forums/en-US/netfxbcl/thread/ddedaf59-fb36-45cd-8062-446fa92d8f68

This article has some great examples that may help you get to a solution whereby you dynamically generate your enum and compile it at runtime. I've done this for an 'eval' type function in VB.Net, but I haven't done it for an enum.

This would not allow for compile time checking of values, so you would be in a world of hurt in a lot of ways there. With the DLR in .Net 4.0, I imagine that you'd have more flexibility (though I admit, I haven't read details of the next version of the framework.)

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