繁体   English   中英

Typesafe枚举还是属性枚举?

[英]Typesafe enum or attribute enum?

我的第一种方法看起来很不错,直到出现运行时错误,我都不知道如何解决var name = ((LCData)attributes[0]).Name; 关于索引超出范围。 实际上,我只是复制在“ 获取Enum的值的属性”中找到的代码因此我不确定100%的实际作用。 因此,当以下代码不起作用时,我转到了另一个解决方案。

public enum Identification : ushort

{

    [LCAttribute("IMG_BG01_Greens")]
    BG01_Greens = 0,

    [LCAttribute("Rabbit", "IMG_E01_Rabbit")]
    ENEMY_E01_Rabbit = 2000,
}

public static class Enums
{
    public static LCData GetInfo(Identification id)
    {
        var type = typeof(Identification);
        var memInfo = type.GetMember(id.ToString());
        var attributes = memInfo[0].GetCustomAttributes(typeof(LCData), false);
        var name = ((LCData)attributes[0]).Name;
        var tex = ((LCData)attributes[0]).Texture;

        LCData data;
        data.Name = name;
        data.Texture = tex;
        return data;
    }
}

public struct LCData
{
    public string Name;
    public string Texture;

    public LCData(Identification id)
    {
        this = Enums.GetInfo(id);
    }
}

public class LCAttribute : System.Attribute
{
    private string _Name;
    public string Name
    {
        get
        {
            return _Name;
        }
    }

    private string _Texture;
    public string Texture
    {
        get
        {
            return _Texture;
        }
    }

    public LCAttribute(string texture)
    {
        _Texture = texture;
    }

    public LCAttribute(string name, string texture)
    {
        _Name = name;
        _Texture = texture;
    }
}

其次,我尝试了类型安全的枚举方法。 这有2个致命的弱点,我找不到以下解决方案:

1)我无法获取可用的枚举条目列表以进行循环操作。

2)我无法通过ID号获得相应的枚举条目。

public sealed class Identification
{

    private readonly ushort _ID;
    private readonly string _Name;
    private readonly string _Tex;

    public static readonly Identification BG01_Greens = new Identification(0, "IMG_BG01_Greens");
    public static readonly Identification ENEMY_E01_Rabbit = new Identification(2000, "Rabbit", "IMG_E01_Rabbit");

    private Identification(ushort id, string tex)
    {
        _ID = id;
        _Tex = tex;
    }

    private Identification(ushort id, string name, string tex)
    {
        _ID = id;
        _Name = name;
        _Tex = tex;
    }
    public ushort ID { get { return _ID; } }
    public string Name { get { return _Name; } }
    public string Texture { get { return _Tex; } }
}

我应该如何进行? 为什么我的第一个解决方案不起作用?

您会混淆LCDataLCAttriubte 因为LCAttribute是有效的属性,但是您尝试使用LCData作为属性。 (顺便说一句,您可能不需要两个单独的类型……但我同意 )。

这是修改后的代码:

public enum Identification : ushort
{
    [LCAttribute("IMG_BG01_Greens")] //Look the type of the attributes is LCAttribute
    BG01_Greens = 0,

    [LCAttribute("Rabbit", "IMG_E01_Rabbit")]
    ENEMY_E01_Rabbit = 2000,
}

public static class Enums
{
    public static LCData GetInfo(Identification id)
    {
        var type = typeof(Identification);
        var memInfo = type.GetMember(id.ToString());
        //this will return an array of LCAttributes
        var attributes = memInfo[0].GetCustomAttributes(typeof(LCAttribute), false);
        //I tell you they are LCAttribute not LCData
        var name = ((LCAttribute)attributes[0]).Name;
        var tex = ((LCAttribute)attributes[0]).Texture;
        //If the above were an LCData why would create a new one here? [Rethorical]
        LCData data;
        data.Name = name;
        data.Texture = tex;
        return data;
    }
}

注意:对于替代方法,也许对这种方法有一些了解,您可以看到我的回答:如何制作与数据相关的“枚举”? 您在此处使用的方法在“自定义属性”下列出。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM