是否有可能保存类型(使用" typeof运算()")在一个枚举? [英] Is it possible to save a Type (using "typeof()") in an enum?

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

问题描述

所以,我创造了XNA游戏,C#4.0中,我需要管理大量的通电(这在代码都是从班的上电继承),并处理通电的后台管理我目前有一个枚举,PowerupEffectType,对每个子类的上电的价值。最后,在我需要从PowerupEffectType做转换到通电的类型(类的代码键入通常实现的typeof([类别名称])

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])).

由于这是一个团体项目,我想PowerupEffectType的每个值嫁给其对应的类类型以及可能的,即:不只是希望我的其他程序员使用switch语句做手工转换,并确保添加/扩展以后包括在尽可能少的地方尽可能少的变化。我有这几个选项,到目前为止,我发现最好的是创建枚举伪的方法都浓缩到一个switch语句(我想要的99%),这得益于一些小技巧,我发现在这里:< A HREF =http://msdn.microsoft.com/en-us/library/bb383974.aspx> http://msdn.microsoft.com/en-us/library/bb383974.aspx

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

不过,我试图把它一步 - 我节省键入枚举 我知道你可以保存为枚举特定类型(链接:http://msdn.microsoft.com/en-us/library/cc138362.aspx ),但键入不是一他们的。目前的选择是的字节,为sbyte,短,USHORT,INT,UINT,long和ulong 的。有没有可行的方法来保存转换一个键入上述任何数据类型和背部?

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?

< STRONG>只要是明确的,这就是我希望我能做到的,我正在寻找一种方式来做到:

// (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)
}

有没有办法做到这一点,还是我只是过于复杂的解决方案,以一个已经完成了99%的问题吗?

Is there any way to do this, or am I just overcomplicating a solution to a problem that's already 99% complete?

在此先感谢!

推荐答案

您不能做到这一点作为的的枚举的,但你可以的在的属性指定它:

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.

这篇关于是否有可能保存类型(使用&QUOT; typeof运算()&QUOT;)在一个枚举?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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