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

前端 未结 7 1069
旧巷少年郎
旧巷少年郎 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));
    }
    
    0 讨论(0)
  • 2021-01-30 10:44

    Sorry, I don't quite follow this, what are you actually trying to achieve; could you give a code excerpt? I'm not sure why you can't just use inheritance here and what an enumeration gives you that type inheritance doesn't. It feels to me like you're presenting the solution rather than the problem, I may be completely wrong, could you clarify how you're planning to use this meta-information?

    I'm confused, are you asking for something that tells you the type of an instance of a type/class? You can use an enumeration to store a list of the types of each type that you say, but why do you want to? You say you don't want to have the other programmers use switch statements, but I'm sorry I can't see what benefit you're getting from storing the type information in some enumeration of... the type. Every instance knows what type it is and what it can do.

    What will you do with the type information? If the types all inherit from a base type, then presumably they have common functionality that can be specified in an abstract method for any special-case handling, or perhaps return a Null Object where there's nothing to do (or maybe just do nothing). This way you don't need to worry about adding new classes- as they must have the necessary behaviour. I try to avoid Enums except in situations where you're actually enumerating a fixed set of arbitrary values, they are inflexible. Enums have to be maintained, which is very difficult (in practice).

    0 讨论(0)
  • 2021-01-30 10:50

    You could use a static Dictionary<PowerupEffectType, Powerup>. I believe that would be the kind of "marriage" you are looking for. It would allow easy enumeration and access.

    0 讨论(0)
  • 2021-01-30 10:50

    You could use only numeric types as by documentation of Microsoft. By default the underlying type of each element in the enum is int. You can specify another integral numeric type by using a colon, as shown in the previous example. For a full list of possible types, see enum. Reference: Enumeration Types

    0 讨论(0)
  • 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();
    
    0 讨论(0)
  • 2021-01-30 10:51

    I actually think you might want to take a look at a dependency injection framework.

    It looks like you are trying to have other developers work on different components and then you are trying to integrate them all at the end in one central location in the code base.

    A few projects to look at:

    • http://www.ninject.org/
    • http://docs.structuremap.net/
    • http://archive.msdn.microsoft.com/mef
    0 讨论(0)
提交回复
热议问题