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

前端 未结 7 1068
旧巷少年郎
旧巷少年郎 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:42

    How about something like this?

    You get the type-safety that you do with an enum, plus implicit conversion to Type

    public class PowerupEffectType
    {
        private readonly Type _powerupType;
    
        public static implicit operator Type(PowerupEffectType powerupEffectType)
        {
            return powerupEffectType._powerupType;
        }
    
        private PowerupEffectType(Type powerupType)
        {
            _powerupType = powerupType;
        }
    
        public static readonly PowerupEffectType LIGHTNING = new PowerupEffectType(typeof(LightningPowerup));
        public static readonly PowerupEffectType FIRE = new PowerupEffectType(typeof(FirePowerup));
        public static readonly PowerupEffectType WATER = new PowerupEffectType(typeof(WaterPowerup));
    }
    

提交回复
热议问题