有什么方法可以将数字的总和转换回枚举? [英] Is there any way to convert a sum of numbers back to enum?
本文介绍了有什么方法可以将数字的总和转换回枚举?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
因此,在名为osu的游戏中有一些模组,当我调用api进行任何游戏时,我都会得到模组编号的总和(例如72)。但是,我想不出任何办法将其转换回原始的枚举名称。
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.
我尝试过这种方式:(Mods)enabled_mods
,但这只会转回数字。
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
}
我想获取已求和的值的名称,例如如果我有72岁,我应该得到HiddenDoubleTime。
I want to get the name of the values that has been summed, like if I have 72 I should get HiddenDoubleTime.
推荐答案
这里是您可以这样做的一种方式:
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();
这篇关于有什么方法可以将数字的总和转换回枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文