Is it possible to save a Type (using “typeof()”) in an enum?

前端 未结 7 1081
旧巷少年郎
旧巷少年郎 2021-01-30 10:32

So I\'m creating a game in XNA, C# 4.0, and I need to manage a lot of PowerUps (which in code are all inherited from class \"PowerUp\"), and to handle back-end management of the

7条回答
  •  旧巷少年郎
    2021-01-30 10:51

    This is not exactly what u are asking for. I find Jon's attribute method the best. But why not wrap it in an extension method?

    public Type GetPowerupEffectType(this PowerupEffectType powerEffect)
    {
        switch (powerEffect)
        {
            case LIGHTNING:
                return typeof(LightningPowerup);
            case FIRE:
                return typeof(FirePowerup);
            case WATER:
                return typeof(WaterPowerup);
            default:
                return default(Type);
        }
    }
    

    And call it:

    PowerupEffectType e = PowerupEffectType.WATER;
    var t = e.GetPowerupEffectType();
    

提交回复
热议问题