C#将其他值与枚举联系起来

时间:2017-08-11 19:16:28

标签: c# .net generics attributes

我在C#和.NET上是绿色的。我试图将其他值与枚举联系起来。我也尝试使用通用函数来检索这些值(来自http://omegacoder.com/?p=28)。通用功能对我不起作用。它只返回Attribute类型的名称。我定义了一个自定义属性,但我不知道如何进入" Num"它的财产。

基本上,如果我有一个CRC多项式枚举值Crc32ieeeNormal(1),我希望能够检索多项式本身。这些必须是单独的数字,因为枚举必须是单字节指示符。

我确定可以使用相同的数字等编写单独的结构,以及我必须做的事情,但是我喜欢这一切捆绑在一起。

using System;
using System.Linq;

namespace csAttributes
{
    using System.Reflection;        // to retrieve attribute values

    class Program
    {
        static void Main(string[] args)
        {
            // I know this isn't right, but can't figure out how to get the attribute value....
            CrcPolynomialTypesEnum crc = CrcPolynomialTypesEnum.Crc32ieeeNormal;
            Console.WriteLine(Utility.ExtractAttribute<U64Attribute, CrcPolynomialTypesEnum>(crc));
            Console.WriteLine(crc.ExtractAttribute<U64Attribute, CrcPolynomialTypesEnum>());

            Console.ReadKey();
        }

        public enum CrcPolynomialTypesEnum : byte
        {
            // These must be explicitly defined as they are values used as indicators in a file.
            [U64(0)]
            Crc32InvalidOrNotSet = 0x00,

            [U64(0x04C11DB7)]
            Crc32ieeeNormal = 0x01,

            [U64(0xEDB88320)]
            Crc32ieeeReversed = 0x02,

            [U64(0x1EDC6F41)]
            Crc32CastagnoliNormal = 0x0A,

            [U64(0x82F63B78)]
            Crc32CastagnoliReversed = 0x0B
        }

    }

    public static class Utility { 
        // from http://omegacoder.com/?p=28, 8/11/2017
        /// <summary>
        /// If an enum has a custom attrbute, this will returrn that attribute or null.
        /// </summary>
        /// <typeparam name="TCustomAttr">The type of the <code class="backtick">custom attribute</code> to extract from the enum.</typeparam>
        /// <typeparam name="TEnumItance">The enum currently being viewed..</typeparam>
        /// <param name="instance">The instance.</param>
        /// <returns>The custom attribute (TCustomAttr) or null</returns>
        public static TCustomAttr ExtractAttribute<TCustomAttr, TEnum>(this TEnum instance)
        {
            if (instance != null)
            {
                try
                {
                    FieldInfo fieldInfo = instance.GetType()
                        .GetField(instance.ToString());

                    var attributes = fieldInfo.GetCustomAttributes(typeof(TCustomAttr), false)
                        .ToList();

                    if (attributes.Any())
                        return (TCustomAttr)attributes[0];

                }
                catch (Exception)
                {
                }
            }

            return default(TCustomAttr);
        }

    }

    [AttributeUsage(AttributeTargets.Field, Inherited = true)]
    public class U64Attribute : Attribute
    {
        private UInt64 n;

        public U64Attribute(UInt64 num)
        {
            n = num;
        }

        public UInt64 Num
        {
            get
            {
                return n;
            }
        }
    }


}

编辑:谢谢你,凯文。有趣的是,&#34; Num&#34;物业早些时候没有,但现在是。一定是我之前没有做过的事情。

编辑2:在这里使用泛型可能毫无意义,并且检索该值的更具体的函数似乎更适合此应用程序。

0 个答案:

没有答案