• c#枚举自定义,用于数据绑定。 z


    [AttributeUsage(AttributeTargets.Field | AttributeTargets.Property | AttributeTargets.Enum)]
            public class EnumSignAttribute : Attribute
            {
                // Fields
                private string _displayName;
    
                public EnumSignAttribute(string displayname)
                {
                    this._displayName = displayname;
                }
    
                // Properties
                public string DisplayName
                {
                    get
                    {
                        return this._displayName;
                    }
                    set
                    {
                        this._displayName = value;
                    }
                }
            }
      
    
    枚举
    
    ?123456789101112public enum DataType   
     {       
     [EnumSign("布尔类型")]        
    Boolean = 4,        
    [EnumSign("日期时间")]        
    DateTime = 3,        
    None = 0,       
     [EnumSign("数字")]        
    Numberic = 2,       
     [EnumSign("字符串")]       
     String = 1    }
    
    public static class EnumsUtils
        {
            /// <summary>
            /// 根据object类型的数据,获取枚举类型
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="enumValue"></param>
            /// <returns></returns>
            public static T ConvertToEnum<T>(object enumValue)
            {
                if (enumValue is int)
                {
                    return (T)enumValue;
                }
                Type type = typeof(T);
                FieldInfo[] fields = type.GetFields();
                int num = 0;
                foreach (FieldInfo info in fields)
                {
                    if (num == 0)
                    {
                        num++;
                    }
                    else
                    {
                        T local = (T)info.GetValue(type);
                        if (local.ToString().Equals(enumValue))
                        {
                            return local;
                        }
                    }
                }
                return default(T);
            }
    
    
            /// <summary>
            /// 根据枚举标识,获取显示名字
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="enumValue">枚举标识</param>
            /// <returns>显示名字</returns>
            public static string GetAttriValue<T>(T enumValue)
            {
                Type type = typeof(T);
                FieldInfo[] fields = type.GetFields();
                int num = 0;
                foreach (FieldInfo info in fields)
                {
                    if (num == 0)
                    {
                        num++;
                    }
                    else
                    {
                        T local = (T)info.GetValue(type);
                        if (local.Equals(enumValue))
                        {
                            return GetDisplayName(info);
                        }
                    }
                }
                return string.Empty;
            }
    
            private static string GetDisplayName(FieldInfo field)
            {
                string displayName = string.Empty;
                object[] arr = field.GetCustomAttributes(typeof(EnumSignAttribute), true);
                if (arr.Length > 0)
                {
                    EnumSignAttribute aa = (EnumSignAttribute)arr[0];
                    displayName = aa.DisplayName;
                }
                return displayName;
            }
    
            /// <summary>
            /// 获取枚举集合列表
            /// </summary>
            /// <param name="type"></param>
            /// <returns></returns>
            public static EnumList GetSourceEnum(Type type)
            {
                EnumList list = new EnumList();
                FieldInfo[] fields = type.GetFields();
                foreach (FieldInfo field in fields)
                {
                    EnumItem item = new EnumItem();
                    if (field.FieldType.IsEnum)
                    {
                        item.Value = ((int)type.InvokeMember(field.Name, BindingFlags.GetField, null, null, null));
                        object[] arr = field.GetCustomAttributes(typeof(EnumSignAttribute), true);
                        if (arr.Length > 0)
                        {
                            EnumSignAttribute aa = (EnumSignAttribute)arr[0];
                            item.Display = aa.DisplayName;
                        }
                        item.Name = field.Name;
                        list.Add(item);
                    }
                }
                return list;
            }
        }
    
        [StructLayout(LayoutKind.Sequential)]
        public struct EnumItem
        {
            private string _display;
            private string _name;
            private object _value;
    
            public EnumItem(string display, string name, object value)
            {
                this._display = display;
                this._name = name;
                this._value = value;
            }
    
            public string Display
            {
                get
                {
                    return this._display;
                }
                set
                {
                    this._display = value;
                }
            }
    
            public string Name
            {
                get
                {
                    return this._name;
                }
                set
                {
                    this._name = value;
                }
            }
    
            public object Value
            {
                get
                {
                    return this._value;
                }
                set
                {
                    this._value = value;
                }
            }
        }
    
    获取枚举集合列表
    
    public class EnumList : BindingList<EnumItem>
        {
        }
    
    //方法1:
    EnumList = EnumsUtils.GetSourceEnum.(typeof(DataType));
    //方法2:
    var  = EnumsUtils.GetSourceEnum.(typeof(DataType));
    
    数据源
    
  • 相关阅读:
    安装MySQL(简便)
    许愿墙的搭建(基于Apache+php+mysql)
    httpd服务的安装、配置和关于php留言本网站的搭建
    Linux系统的初化始配置(包括配置网络,修改主机名,关闭firewalld与selinux的生效)
    Linux系统root用户忘记密码的重置方法
    Linux下面CentOS 7桌面的安装
    通过挂载系统光盘搭建本地yum仓库的方法
    时间同步ntp服务的安装与配置(作为客户端的配置)
    larbin
    Larbin的安装与配置
  • 原文地址:https://www.cnblogs.com/zeroone/p/4298467.html
Copyright © 2020-2023  润新知