有没有办法将数字总和转换回枚举? [英] 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屋!
查看全文