C#: Wrapping one Enum inside another (ie. mirroring another enum/copying it...)
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
{
开发者_如何学Pythonvalue1,
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)
}
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
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.)
精彩评论