问题
So there are some mods in a game called osu and when I call the api for any plays I get back a sum of the mod's number (like 72). However, I can't think of any way to convert that back to the original enum names.
I tried this way: (Mods)enabled_mods
, but this only turned back the number.
enum Mods
{
None = 0,
NoFail = 1,
Easy = 2,
TouchDevice = 4,
Hidden = 8,
HardRock = 16,
SuddenDeath = 32,
DoubleTime = 64,
Relax = 128,
HalfTime = 256,
Nightcore = 512, // Only set along with DoubleTime. i.e: NC only gives 576
Flashlight = 1024,
Autoplay = 2048,
SpunOut = 4096,
Relax2 = 8192, // Autopilot
Perfect = 16384, // Only set along with SuddenDeath. i.e: PF only gives 16416
Key4 = 32768,
Key5 = 65536,
Key6 = 131072,
Key7 = 262144,
Key8 = 524288,
FadeIn = 1048576,
Random = 2097152,
Cinema = 4194304,
Target = 8388608,
Key9 = 16777216,
KeyCoop = 33554432,
Key1 = 67108864,
Key3 = 134217728,
Key2 = 268435456,
ScoreV2 = 536870912,
LastMod = 1073741824,
KeyMod = Key1 | Key2 | Key3 | Key4 | Key5 | Key6 | Key7 | Key8 | Key9 | KeyCoop,
FreeModAllowed = NoFail | Easy | Hidden | HardRock | SuddenDeath | Flashlight | FadeIn | Relax | Relax2 | SpunOut | KeyMod,
ScoreIncreaseMods = Hidden | HardRock | DoubleTime | Flashlight | FadeIn
}
I want to get the name of the values that has been summed, like if I have 72 I should get HiddenDoubleTime.
回答1:
Here's one way you could do this:
Mods selectedMods = (Mods)72;
var individualMods = Enum
.GetValues(typeof(Mods))
.Cast<Mods>()
.Where(mod => selectedMods.HasFlag(mod) && mod != Mods.None)
.ToList();
来源:https://stackoverflow.com/questions/57574668/is-there-any-way-to-convert-a-sum-of-numbers-back-to-enum