• c# 集合适配器


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Reflection;
    using System.Text;
    using System.Threading.Tasks;
    
    namespace ConsoleApplication1
    {
        public class EnumRemarkAttribute : Attribute
        {
            private string _description;
    
            public string Description
            {
                get
                {
                    return _description;
    
                }
                set { _description = value; }
            }
            public EnumRemarkAttribute(string description)
            {
                _description = description;
            }
        }
    
        public enum Color
        {
            [EnumRemark("金色")]
            Gold=0,
            [EnumRemark("黄色")]
            Huang=1,
            [EnumRemark("白色")]
            bai=2,
            [EnumRemark("黑色")]
            hei=3
        }
    
     
    
        /// <summary>
        /// 集合适配器
        /// </summary>
    
        public class EnumDataSource<EnumType> : List<EnumDataSource<EnumType>.EnumAdapter>
        {
            public EnumDataSource()
            {
                if (!typeof(EnumType).IsEnum)
                    throw new NotSupportedException("Can not support type: " + typeof(EnumType).FullName);
                foreach (EnumType enumType in Enum.GetValues(typeof(EnumType)))
                    this.Add(new EnumAdapter(enumType));
            }
            /// <summary>
            /// 包装适配类
            /// </summary>
            public sealed class EnumAdapter
            {
                //被包装的东西,原材料
                private EnumType _value;
    
                public EnumType Value
                {
                    get
                    {
                        return this._value;
                    }
                }
    
                public int EnumValue
                {
                    get
                    {
                        return Convert.ToInt32((object)this._value);
                    }
                }
    
                public string EnumName
                {
                    get
                    {
                        string _enumName="";
                        FieldInfo field = typeof(EnumType).GetField(_value.ToString());
                        var arr = field.GetCustomAttributes(typeof (EnumRemarkAttribute));
                        foreach (EnumRemarkAttribute item  in arr)
                        {
                            _enumName = item.Description;
                            break;
                        }
                        return _enumName;
                    }
    
                }
    
    
                public EnumAdapter(EnumType value)
                {
                    if (!Enum.IsDefined(typeof(EnumType), (object)value))
                        throw new ArgumentException(string.Format("{0} is not defined in {1}", (object)value, (object)typeof(EnumType).Name), "value");
                    this._value = value;
                }
            }
        }
    
    
    
        class Program
        {
            static void Main(string[] args)
            {
    
                IList<EnumDataSource<Color>.EnumAdapter> list=new EnumDataSource<Color>();
                foreach (EnumDataSource<Color>.EnumAdapter a in list)
                {
                    Console.WriteLine(a.EnumValue+":"+a.EnumName);
                    
                }
                Console.ReadKey();
            }
        }
    }
  • 相关阅读:
    记录一次使用npm包管理器的过程
    Markdown---锚点
    Tomcat应用部署
    记录项目中使用SoapUI进行webservice接口测试
    python--使用logging库自定义日志输出
    python--configparser库处理配置文件
    python--excel操作插件openpyxl
    python--使用pycharm调试代码
    【云剪贴板】你不要点开啊!!!!
    【每天一个爆零小技巧】个人用的程序小技巧及其他_自用
  • 原文地址:https://www.cnblogs.com/kexb/p/6072755.html
Copyright © 2020-2023  润新知