从派生类中运行时施放特定的枚举
我有一组从同一基类得出的类,所有这些都定义了同名的独特枚举。
public class DerivedClassOne : BaseClass
{
public enum Flags
{
None = 0,
cheese = 1,
ham = 2,
egg = 4
}
}
public class DerivedClassTwo : BaseClass
{
public enum Flags
{
None = 0,
bacon = 1,
lettuce = 2,
tomato = 4
}
}
我只有一个Baseclass
的实例能够将INT(n)投放给该枚举并在运行时存储? IE:
object flag = (BaseClassInstance.Enums.Flags)n;
I have a group of classes that all are derived from the same base class, and all define unique Enums of the same name.
public class DerivedClassOne : BaseClass
{
public enum Flags
{
None = 0,
cheese = 1,
ham = 2,
egg = 4
}
}
public class DerivedClassTwo : BaseClass
{
public enum Flags
{
None = 0,
bacon = 1,
lettuce = 2,
tomato = 4
}
}
How would I, with only an instance of BaseClass
, be able to cast an int (n) to this enum and store it at runtime? ie:
object flag = (BaseClassInstance.Enums.Flags)n;
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您不介意 /a> enums作为
enum
(比object
>),然后您可以执行此操作:屏幕截图证明:
If you don't mind boxing enums as
Enum
(which is somewhat better thanObject
), then you can do this:Screenshot proof: