C#:将一个枚举包装在另一个枚举中(即镜像另一个枚举/复制它......)
这是我的问题:我有一个引用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您所问的问题已在此处讨论:
枚举“继承”
What you are asking has been discussed here:
Enum "Inheritance"
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.)