自定属性的描叙信息类

  [PropertyDescription("物料编号", ProperVlaue = "MATERIAL_ID", DefaultValue = "MaterialId")]
        public int MATERIAL_ID { get; set; }
用来自定义属性的描叙信息,该属性信息可以通过反射获取,自定义的类如下:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Collections;


namespace SKY.DEFINITION
{

     /// <summary>  
    /// 范小军
    /// </summary>
    public enum EnumDBFieldUsage
    {
        /// <summary>
        /// 未定义。
        /// </summary>
        None = 0x00,
        /// <summary>
        /// 用于主键。
        /// </summary>
        PrimaryKey = 0x01,
        /// <summary>
        /// 用于唯一键。
        /// </summary>
        UniqueKey = 0x02,
        /// <summary>
        /// 由系统控制该字段的值。
        /// </summary>
        BySystem = 0x04
    }
    /// <summary>
    /// 用来获取属性描述信息的类
    /// 范小军
    /// 2012-01-05
    /// </summary>
    [AttributeUsage(AttributeTargets.Property, Inherited = true)]
    public class PropertyDescriptionAttribute : Attribute
    {

        EnumDBFieldUsage m_usage;
        string m_strFieldName;
        string m_strDescription;
        string m_strProperName;
        object m_defaultValue;

        public PropertyDescriptionAttribute(string strFieldName, object defaultValue, EnumDBFieldUsage usage, string strDescription)
        {
            m_strFieldName = strFieldName;
            m_defaultValue = defaultValue;
            m_usage = usage;
            m_strDescription = strDescription;
        }

        public PropertyDescriptionAttribute(string strFieldName,string strProperName, object defaultValue, EnumDBFieldUsage usage, string strDescription)
        {
            m_strFieldName = strFieldName;
            m_strProperName = strProperName;
            m_defaultValue = defaultValue;
            m_usage = usage;
            m_strDescription = strDescription;
        }

        public PropertyDescriptionAttribute(string fieldName)
            : this(fieldName, null, EnumDBFieldUsage.None, null)
        { }

        public PropertyDescriptionAttribute(string fieldName, EnumDBFieldUsage usage)
            : this(fieldName, null, usage, null)
        { }


        // 获取该成员映射的数据库字段名称。
        public string FieldName
        {
            get
            {
                return m_strFieldName;
            }
            set
            {
                m_strFieldName = value;
            }
        }

        // 获取该字段的默认值
        public object DefaultValue
        {
            get
            {
                return m_defaultValue;
            }
            set
            {
                m_defaultValue = value;
            }
        }

        // 获取该字段的默认值
        public string  ProperVlaue
        {
            get
            {
                return m_strProperName;
            }
            set
            {
                m_strProperName = value;
            }
        }
    }
}

使用方法如下:

  [PropertyDescription("物料编号", ProperVlaue = "MATERIAL_ID", DefaultValue = "MaterialId")]
        public int MATERIAL_ID { get; set; }

 可以通过前一章的反射类,获取相应的信息,方法如下:

      /// <summary>
        /// 获取一个属性的类型
        /// </summary>
        /// <param name="NameSpacestr"></param>
        /// <param name="obj"></param>
        /// <param name="PropertyName"></param>
        /// <returns></returns>
        public string getPropertyType(string NameSpacestr, string PropertyName)
        {

            Type t = Type.GetType(NameSpacestr);
            string tempvalue = "";  
            foreach (PropertyInfo pi in t.GetProperties(BindingFlags.Instance | BindingFlags.Public))
            {
                List<string> lpropername = new List<string>();
                List<string> ldefault = new List<string>();
                string temp = "";
                object[] attrs = pi.GetCustomAttributes(typeof(PropertyDescriptionAttribute), true);
                if (attrs.Length == 1)
                {
                    PropertyDescriptionAttribute attr = (PropertyDescriptionAttribute)attrs[0];
                    lpropername.Add(attr.ProperVlaue);
                    ldefault.Add(attr.DefaultValue.ToString());    
                }
                
                for (int i = 0; i < lpropername.Count; i++) {
                    if (ldefault[i].Equals(PropertyName))
                    {
                    temp = lpropername[i];
                }
                
                }
                if (pi.Name.Equals(temp))
                {
                    tempvalue = pi.PropertyType.Name.ToString();
                    break;
                }

            }
            return tempvalue;

        }

 可以获取响应的信息。

原文地址:https://www.cnblogs.com/fanxiaojun/p/2385693.html