C# 类型安全枚举还是属性枚举?

C# 类型安全枚举还是属性枚举?,c#,enums,attributes,typesafe,C#,Enums,Attributes,Typesafe,我的第一种方法似乎可以很好地工作,直到我在var name=LCDataattributes[0]上遇到一个运行时错误时才知道如何解决;关于索引超出范围。实际上,我只是在复制我在网站上找到的代码,所以我不能100%确定它到底做了什么。因此,当下面的代码不起作用时,我转向另一个解决方案 public enum Identification : ushort { [LCAttribute("IMG_BG01_Greens")] BG01_Greens = 0, [LCA

我的第一种方法似乎可以很好地工作,直到我在var name=LCDataattributes[0]上遇到一个运行时错误时才知道如何解决;关于索引超出范围。实际上,我只是在复制我在网站上找到的代码,所以我不能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;
    }
}
其次,我尝试了类型安全枚举方法。这有两个致命弱点,我无法找到解决方案:

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; } }
}
我应该如何进行?为什么我的第一个解决方案不起作用?

您混淆了LCData和LCAttriubte。因为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;
    }
}

注意:对于替代方法,您可以看到一些关于这种方法的见解。这里使用的方法列在“自定义属性”下。

为什么不使用字典呢?它是类型安全、闪电般快速的,您可以通过它进行枚举。类似:。虽然这不是基于属性。我以前尝试将其强制转换为LCAttribute,但它给出了索引超出范围的错误,因此在绝望的尝试中,我也尝试将其强制转换为LCData,它遵循复制的代码版本,但现在看起来我的问题可以通过一个简单的字典解决,谢谢帮助@user3110376如果您获取的索引超出范围,则可能意味着您无法读取该属性,就好像它不存在一样。链接答案中的备选方案对该情况有一些额外检查。