是否可以在枚举中保存一个Type(使用“typeof()”)? [英] Is it possible to save a Type (using "typeof()") in an enum?

查看:151
本文介绍了是否可以在枚举中保存一个Type(使用“typeof()”)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在XNA,C#4.0中创建一个游戏,我需要管理大量的PowerUps(代码中都是继承自PowerUp类),并且处理PowerUps I的后端管理当前有一个枚举,PowerupEffectType,具有PowerUp的每个子类的值。最终在代码中,我需要从PowerupEffectType转换为Powerup类型(类 Type ,通常使用 typeof([class name]))。



由于这是一个组项目,我想要将PowerupEffectType的每个值与其相应的类类型结合起来,也就是:不只是期望我的其他程序员使用switch语句来手动进行转换,并且确保添加/扩展稍后在尽可能少的地方涉及少量更改。我有几个选择,迄今为止我发现的最好的是创建枚举伪方法,将所有内容都集中到一个switch语句(我想要的99%),感谢我在这里找到的一些提示: a href =http://msdn.microsoft.com/en-us/library/bb383974.aspx =noreferrer> http://msdn.microsoft.com/en-us/library/bb383974.aspx



但是我想再进一步 - 我可以保存一个类型枚举我知道您可以将枚举作为特定类型保存(链接: http://msdn.microsoft.com/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: http://msdn.microsoft.com/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: http://msdn.microsoft.com/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()”)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆