简体   繁体   English

使用 C# 中的自定义属性将标头信息添加到枚举

[英]Adding header information to an enum using a custom attribute in C#

I am able to create and build my own attributes and use them from within an enum, but I am unable to use these attributes as header information to an enum.我能够创建和构建自己的属性并在枚举中使用它们,但我无法将这些属性用作枚举的标头信息。

For instance, consider the following enum:例如,考虑以下枚举:

    [EnumSize(0x30000)]
    public enum Memories
    { 
        [MemSize(0x10000)]
        [Description("Memory0")]
        Memory0Base = 0x00000,

        [MemSize(0x10000)]
        [Description("Memory1")]
        Memory1Base= 0x10000,

        [MemSize(0x10000)]
        [Description("Memory2")]
        Memory2Base= 0x20000
    }

Now, I have created both EnumSize and MemSize attributes in the following way:现在,我通过以下方式创建了 EnumSize 和 MemSize 属性:

public class EnumSizeAttribute : System.Attribute
{
    private int _value;
    public EnumSizeAttribute(int value)
    {
        _value = value;
    }
    public int Value
    {
        get { return _value; }
    }
}

And made it more "access friendly" (meaning I can do something like this: Memories.Memory2Base.MemSize() or Memories.EnumSize()):并使其更加“访问友好”(意味着我可以执行以下操作:Memories.Memory2Base.MemSize() 或 Memories.EnumSize()):

public static string EnumSize<T>(this T value)
{
    FieldInfo fi = value.GetType().GetField(value.ToString());

    EnumSizeAttribute[] attributes = (EnumSizeAttribute[])fi.GetCustomAttributes(typeof(EnumSizeAttribute), false);

    if (attributes.Length > 0)
        return attributes[0].EnumSize();
    else
        return value.ToString();
}

Now, although it generates no compilation error when using it as a header to my enum as here below, I am unable to get the EnumSize by doing "Memories.EnumSize()".现在,虽然在将其用作我的枚举的标头时不会产生编译错误,如下所示,但我无法通过执行“Memories.EnumSize()”来获得 EnumSize。

[EnumSize(0x30000)]
public enum Memories
{ 
 ...
}

Any tip on how to get it to work would be very much appreciated.非常感谢有关如何使其工作的任何提示。

Memories is a type not a field, so you would need to do something like this: Memories是一种类型而不是字段,因此您需要执行以下操作:

public static string EnumSize<T>() where T : struct, Enum
{
    EnumSizeAttribute attribute = typeof(T).GetCustomAttribute<EnumSizeAttribute>();

    return attribute?.Value.ToString();
}

And use as follows:并使用如下:

string size = EnumSize<Memories>();

You won't be able to call this as an extension method like you have with MemSize though.但是,您将无法像使用MemSize那样将其称为扩展方法。

The only way to do that would be to extend Type , for example:做到这一点的唯一方法是扩展Type ,例如:

public static string EnumSize(this Type type)
{
    EnumSizeAttribute attribute = type.GetCustomAttribute<EnumSizeAttribute>();

    return attribute?.Value.ToString();
}

string size = Memories.EnumSize();

But that would be available to use with all types, not just enums, which is probably not what you want.但这可用于所有类型,而不仅仅是枚举,这可能不是您想要的。

Of course you could throw an exception if it is used incorrectly:当然,如果使用不当,您可能会抛出异常:

public static string EnumSize(this Type type)
{
    if (!type.IsEnum)
    {
        throw new InvalidOperationException("EnumSize only applies to enums");
    }

    EnumSizeAttribute attribute = type.GetCustomAttribute<EnumSizeAttribute>();

    return attribute?.Value.ToString();
}

But I would probably stick to the first example.但我可能会坚持第一个例子。

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

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