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

25 篇文章 2 订阅
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;
}

结果:

 

 

 

  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Bridge_go

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值