是否可以在枚举中保存一个Type(使用“typeof()”)?

编程入门 行业动态 更新时间:2024-10-23 01:46:01
本文介绍了是否可以在枚举中保存一个Type(使用“typeof()”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 所以我在XNA,C#4.0中创建一个游戏,我需要管理大量的PowerUps(代码中都是继承自PowerUp类),并且处理PowerUps I的后端管理当前有一个枚举,PowerupEffectType,具有PowerUp的每个子类的值。最终在代码中,我需要从PowerupEffectType转换为Powerup类型(类 Type ,通常使用 typeof([class name]))。

由于这是一个组项目,我想要将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()”)?

本文发布于:2023-10-23 02:06:40,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1519420.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:Type   typeof

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!