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; }
结果:
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· .NET10 - 预览版1新功能体验(一)