由于这是一个组项目,我想要将PowerupEffectType的每个值与其相应的类类型结合起来,也就是:不只是期望我的其他程序员使用switch语句来手动进行转换,并且确保添加/扩展稍后在尽可能少的地方涉及少量更改。我有几个选择,迄今为止我发现的最好的是创建枚举伪方法,将所有内容都集中到一个switch语句(我想要的99%),感谢我在这里找到的一些提示: a href =msdn.microsoft/en-us/library/bb383974.aspx =noreferrer> msdn.microsoft/en-us/library/bb383974.aspx
但是我想再进一步 - 我可以保存一个类型在枚举?我知道您可以将枚举作为特定类型保存(链接: msdn.microsoft/en-us/library/cc138362.aspx ),但 Type 不是其中之一。当前的选择是字节,字节,短,ushort,int,uint,long和ulong 。有没有可行的方式来保存转换为类型到任何上述数据类型,并返回?
要清楚,这就是我可以做的事情,我正在寻找一种方法:
//(假设LightningPowerup,FirePowerup和WaterPowerup是 //所有声明的类继承自单个基类) public enum PowerupEffectType { LIGHTNING = typeof(LightningPowerup), FIRE = typeof(FirePowerup), WATER = typeof(WaterPowerup)}有没有办法做到这一点,或者我只是解决了已经99%完成的问题的解决方案?
提前感谢
解决方案你不能像值,但您可以在属性中指定:
使用系统; 使用System.Runtime.CompilerServices; [AttributeUsage(AttributeTargets.Field)] public class EffectTypeAttribute:Attribute { public Type Type {get;私人集合 public EffectTypeAttribute(Type type) { this.Type = type; } } public class LightningPowerup {} public class FirePowerup {} public class WaterPowerup {} public枚举PowerupEffectType { [EffectType(typeof(LightningPowerup))] 闪电, [EffectType(typeof(FirePowerup))] Fire, [ EffectType(typeof(WaterPowerup))] 水}通过反射在执行时提取这些属性值。但是,我会个人只需创建一个字典:
private static Dictionary< PowerupEffectType,Type> EffectTypeMapping = new Dictionary< PowerupEffectType,Type> { {PowerupEffectType.Lightning,typeof(LightningPowerup)}, {PowerupEffectType.Fire,typeof(FirePowerup)}, {PowerupEffectType.Water,typeof(WaterPowerup)} };无需特殊属性,无需使用虚拟反思代码提取值。
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 PowerUps I currently have an enum, PowerupEffectType, with a value for each child class of PowerUp. Eventually in the code I need to do conversions from PowerupEffectType to the Powerup type (of class Type, achieved usually with typeof([class name])).
Since this is a group project, I want to marry each value of PowerupEffectType to its corresponding class Type as well as possible, i.e.: Not just expect my other programmers to use switch statements to do the conversion manually, and making sure additions/expansions later involve as few changes in as few places as possible. I have a few options for this, and the best I've discovered so far is creating enum pseudo-methods that condense everything down to a single switch statement (99% of what I want), thanks to some tips I found here: msdn.microsoft/en-us/library/bb383974.aspx
But I'm trying to take it one step further - can I save a Type in an enum? I know you can save enums as a specific type (link: msdn.microsoft/en-us/library/cc138362.aspx), but Type isn't one of them. The current choices are byte, sbyte, short, ushort, int, uint, long, and ulong. Is there any feasible way to save a convert a Type to any of the above data types and back?
Just to be clear, this is what I WISH I could do, and I'm looking for a way to do:
// (Assuming 'LightningPowerup', 'FirePowerup', and 'WaterPowerup' are // all declared classes that inherit from a single base class) public enum PowerupEffectType { LIGHTNING = typeof(LightningPowerup), FIRE = typeof(FirePowerup), WATER = typeof(WaterPowerup) }Is there any way to do this, or am I just overcomplicating a solution to a problem that's already 99% complete?
Thanks in advance!
解决方案You can't do it as the value of an enum, but you could specify it in an attribute:
using System; using System.Runtime.CompilerServices; [AttributeUsage(AttributeTargets.Field)] public class EffectTypeAttribute : Attribute { public Type Type { get; private set; } public EffectTypeAttribute(Type type) { this.Type = type; } } public class LightningPowerup {} public class FirePowerup {} public class WaterPowerup {} public enum PowerupEffectType { [EffectType(typeof(LightningPowerup))] Lightning, [EffectType(typeof(FirePowerup))] Fire, [EffectType(typeof(WaterPowerup))] Water }You can then extract those attribute values at execution time with reflection. However, I would personally just create a dictionary:
private static Dictionary<PowerupEffectType, Type> EffectTypeMapping = new Dictionary<PowerupEffectType, Type> { { PowerupEffectType.Lightning, typeof(LightningPowerup) }, { PowerupEffectType.Fire, typeof(FirePowerup) }, { PowerupEffectType.Water, typeof(WaterPowerup) } };No need for a special attribute, no need to extract the values with fiddly reflection code.
更多推荐
是否可以在枚举中保存一个Type(使用“typeof()”)?
发布评论