• C#针对PropertyGrid控件修改其常用的几个特性的值


    using System;
    using System.ComponentModel;
    using System.Reflection;
    
    namespace PropertyGridUse
    {
        public class PropertyAttribute<T>
        {
            /// <summary>
            /// 修改propertyName名称的属性Category特性的值
            /// </summary>
            public bool SetCategory(string propertyName, string value)
            {
                return SetAttributes<CategoryAttribute>(propertyName, value);
            }
    
            /// <summary>
            /// 修改propertyName名称的属性Description特性的值
            /// </summary>
            public bool SetDescription(string propertyName, string value)
            {
                return SetAttributes<DescriptionAttribute>(propertyName, value);
            }
    
            /// <summary>
            /// 修改propertyName名称的属性DisplayName特性的值
            /// </summary>
            public bool SetDisplayName(string propertyName, string value)
            {
                return SetAttributes<DisplayNameAttribute>(propertyName, value);
            }
    
            /// <summary>
            /// 修改propertyName名称的属性ReadOnly特性的值
            /// </summary>
            public bool SetReadOnly(string propertyName, bool value)
            {
                return SetAttributes<ReadOnlyAttribute>(propertyName, value);
            }
    
            /// <summary>
            /// 修改propertyName名称的属性Browsable特性的值
            /// </summary>
            public bool SetBrowsable(string propertyName, bool value)
            {
                return SetAttributes<BrowsableAttribute>(propertyName, value);
            }
    
            PropertyInfo GetPropertyInfo(string propertyName)
            {
                PropertyInfo[] propertyInfos = typeof(T).GetProperties();
                foreach (PropertyInfo propertyInfo in propertyInfos)
                {
                    if (propertyInfo.Name == propertyName)
                    {
                        return propertyInfo;
                    }
                }
                return null;
            }
    
            /// <summary>
            /// propertyName名称的属性是否存在
            /// </summary>
            /// <param name="propertyName"></param>
            /// <returns></returns>
            public bool IsExist(string propertyName)
            {
                return GetPropertyInfo(propertyName) != null;
            }
    
            /// <summary>
            /// propertyName名称的属性是否存在A类型的特性
            /// </summary>
            public bool IsExist<A>(string propertyName)
            {
                PropertyInfo propertyInfo = GetPropertyInfo(propertyName);
                if (propertyInfo != null)
                {
                    Type attributeType = typeof(A);
                    var attr = Attribute.GetCustomAttribute(propertyInfo, attributeType);
                    if (attr != null)
                    {
                        return true;
                    }
                }
                return false;
            }
    
            bool SetAttributes<A>(string propertyName, object value)
            {
                PropertyInfo propertyInfo = GetPropertyInfo(propertyName);
                if (propertyInfo != null)
                {
                    Type attributeType = typeof(A);
                    var attr = Attribute.GetCustomAttribute(propertyInfo, attributeType);
                    if (attr != null)
                    {
                        string attributeFieldName = string.Empty;
                        switch (attributeType.Name)
                        {
                            case "CategoryAttribute": attributeFieldName = "categoryValue"; break;
                            case "DescriptionAttribute": attributeFieldName = "description"; break;
                            case "DisplayNameAttribute": attributeFieldName = "_displayName"; break;
                            case "ReadOnlyAttribute": attributeFieldName = "isReadOnly"; break;
                            case "BrowsableAttribute": attributeFieldName = "browsable"; break;
                            default:
                                throw new Exception(string.Format("不能设置{0}的值", attributeType.Name));
                        }
                        PropertyDescriptorCollection attributes = TypeDescriptor.GetProperties(typeof(T));
                        FieldInfo fieldInfo = attributeType.GetField(attributeFieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
                        fieldInfo.SetValue(attributes[propertyInfo.Name].Attributes[attributeType], value);
                        return true;
                    }
                    throw new Exception(string.Format("{0}不存在{1}特性", propertyName, attributeType.Name));
                }
                throw new Exception(string.Format("不存在{0}属性", propertyName));
            }
    
            bool AddAttributes<A>(string propertyName, object value)
            {
                Type attributeType = typeof(A);
                string attributeFieldName = string.Empty;
                switch (attributeType.Name)
                {
                    case "CategoryAttribute": attributeFieldName = "category"; break;
                    case "DescriptionAttribute": attributeFieldName = "description"; break;
                    case "DisplayNameAttribute": attributeFieldName = "displayName"; break;
                    //case "ReadOnlyAttribute": attributeFieldName = "isReadOnly"; break;
                    //case "BrowsableAttribute": attributeFieldName = "browsable"; break;
                    default:
                        throw new Exception(string.Format("不能添加{0}的值", attributeType.Name));
                }
                if (IsExist<A>(propertyName))
                {
                    throw new Exception(string.Format("已存在{0},不能继续添加", attributeType.Name));
                }
                PropertyDescriptorCollection attributes = TypeDescriptor.GetProperties(typeof(T));
                Type memberDescriptorType = typeof(MemberDescriptor);
                FieldInfo fieldInfo = memberDescriptorType.GetField(attributeFieldName, BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
                fieldInfo.SetValue(attributes[propertyName], value);
                return true;
            }
    
            /// <summary>
            /// 给propertyName名称的属性添加Category特性
            /// </summary>
            public bool AddCategory(string propertyName, string value)
            {
                return AddAttributes<CategoryAttribute>(propertyName, value);
            }
            /// <summary>
            /// 给propertyName名称的属性添加Description特性
            /// </summary>
            public bool AddDescription(string propertyName, string value)
            {
                return AddAttributes<DescriptionAttribute>(propertyName, value);
            }
            /// <summary>
            /// 给propertyName名称的属性添加DisplayName特性
            /// </summary>
            public bool AddDisplayName(string propertyName, string value)
            {
                return AddAttributes<DisplayNameAttribute>(propertyName, value);
            }
    
            [Obsolete("没找到合适方法添加ReadOnly特性")]
            public bool AddReadOnly(string propertyName, bool value)
            {
                //return AddAttributes<ReadOnlyAttribute>(propertyName, value);
                return true;
            }
    
            [Obsolete("没找到合适方法添加Browsable特性")]
            public bool AddBrowsable(string propertyName, bool value)
            {
                //return AddAttributes<BrowsableAttribute>(propertyName, value);
                return true;
            }
    
        }
    }
    

    测试: 

    class People
    {
        [Category("信息"), DisplayName("身份ID")]
        public int ID { get; set; }
    
        [Category("信息"), DisplayName("年龄"), ReadOnly(false)]
        public int Age { get; set; }
    }
    
    private void Form1_Load(object sender, EventArgs e)
    {
        PropertyAttribute<People> propertyAttribute = new PropertyAttribute<People>();
        propertyAttribute.SetDisplayName("ID", "身份证号");
        propertyAttribute.SetReadOnly("Age", true);
        propertyAttribute.AddDescription("ID", "唯一值");
        People config = new People();
        this.propertyGrid1.SelectedObject = config;
    }

    结果:

  • 相关阅读:
    python_接口基础知识
    python_基础总结
    python_配置文件_yaml
    python_loggin日志处理
    python_数据驱动_ddt
    python_unittest_单元测试_openpyxl
    python_类与对象总结_继承
    python_路径操作及类和对象
    python_导包
    Codeforces Round #655 (Div. 2) B. Omkar and Last Class of Math
  • 原文地址:https://www.cnblogs.com/bridgew/p/16138052.html
Copyright © 2020-2023  润新知