Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/string/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
String 如何将枚举字符串值转换为变量?_String_Variables_Enums - Fatal编程技术网

String 如何将枚举字符串值转换为变量?

String 如何将枚举字符串值转换为变量?,string,variables,enums,String,Variables,Enums,假设我有这个枚举 public enum StatType { Strength, Dexterity, Intelligence, Wisdom, Luck } 我有这个功能 public void SetStats(StatType type, int value) { switch (type) { case StatType.Strength: hero.Stats.Strength += value;

假设我有这个枚举

public enum StatType
{
Strength, Dexterity, Intelligence, Wisdom, Luck
}
我有这个功能

public void SetStats(StatType type, int value)
{
    switch (type)
    {
            case StatType.Strength:
            hero.Stats.Strength += value;
            break;
            case StatType.Dexterity:
            hero.Stats.Dexterity += value;
            break;
            case StatType.Intelligence:
            hero.Stats.Intelligence += value;
            break;
            case StatType.Wisdom:
            hero.Stats.Wisdom += value;
            break;
            case StatType.Luck:
            hero.Stats.Luck += value;
            break;
    } 
}
如何将这个传入的stat的“type”参数更改为hero.Stats。()变量的名称


如果这是可能的,那么我就不需要使用switch,对吗?

您可以使用反射来实现,但这会使程序更加复杂。你的代码很好。是的,有了反射,你可以在没有开关的情况下使用nandsito Thx,好的,我明白了。那么我应该对此感到满意了。