zoukankan      html  css  js  c++  java
  • 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();
            }
        }
    }
  • 相关阅读:
    java小知识点5
    java小知识点4
    java小知识点3
    编程之法:面试和算法心得(寻找最小的k个数)
    389. Find the Difference
    104. Maximum Depth of Binary Tree
    485. Max Consecutive Ones
    693. Binary Number with Alternating Bits
    463. Island Perimeter
    566. Reshape the Matrix
  • 原文地址:https://www.cnblogs.com/kexb/p/6072755.html
Copyright © 2011-2022 走看看