有什么方法可以在运行时更改 enum
值?
例如我有以下类型
enum MyType
{
TypeOne, //=5 at runtime
TypeTwo //=3 at runtime
}
我想在运行时将 5 设置为 TypeOne
,将 3 设置为 TypeTwo
。
最佳答案
正如其他人所指出的,答案是否定的。
但是您可以重构您的代码以改用类:
public sealed class MyType
{
public int TypeOne { get; set; }
public int TypeTwo { get; set; }
}
...
var myType = new MyType { TypeOne = 5, TypeTwo = 3 };
或该主题的变体。
关于c# - 在 C# 中的运行时设置枚举值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26827461/