• 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();
            }
        }
    }
  • 相关阅读:
    如何选择RabbitMQ的消息保存方式?
    一次Flannel和Docker网络不通定位问题
    flannel
    vsftp设置不同用户登录ftp的根目录不同
    nginx反向代理服务器获取不到端口的问题的解决办法
    RabbitMQ 内存控制 硬盘控制
    -bash: fork: Cannot allocate memory
    redis info
    nginx第三方模块---nginx-sticky-module的使用(基于cookie的会话保持)
    Haproxy的三种保持客户端会话保持方式
  • 原文地址:https://www.cnblogs.com/kexb/p/6072755.html
Copyright © 2020-2023  润新知