110,533
社区成员
发帖
与我相关
我的任务
分享
class Item
{
public int ItemType
{
get;
set;
}
[Browsable(true)]
public string ItemName
{
get;
set;
}
}
using System.ComponentModel;
using System.Reflection;
private void SetPropertyReadOnly(object obj, string propertyName, bool readOnly)
{
try
{
Type type = typeof(ReadOnlyAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("isReadOnly", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
fld.SetValue(attrs[type], readOnly);
}
catch (Exception ex)
{
SATLogger.LogException(ex);
}
}
private void SetPropertyVisibility(object obj, string propertyName, bool visible)
{
try
{
Type type = typeof(BrowsableAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic);
fld.SetValue(attrs[type], visible);
}
catch (Exception ex)
{
SATLogger.LogException(ex);
}
}
//rule 是你的属性的归属对象的实例,它是object类型,你的类型是什么你自己应该知道吧
//"ExcludeList" 属性的名称,在你这里应该是"ItemName"
//第三个参数指定你要改变为何值,这里是true or false
SetPropertyVisibility(rule, "ExcludeList", true);