C# 属性网格,是否将browsable属性设置为运行时枚举每个项?
我有一个关于c#属性网格的问题C# 属性网格,是否将browsable属性设置为运行时枚举每个项?,c#,enums,attributes,propertygrid,browsable,C#,Enums,Attributes,Propertygrid,Browsable,我有一个关于c#属性网格的问题 public enum xxx { [Browsable(true)] aaa, [Browsable(false)] bbb, [Browsable(true)] ccc, } public class testObject { public xxx temp; public xxx test { get { return temp; } set { temp = value;
public enum xxx
{
[Browsable(true)]
aaa,
[Browsable(false)]
bbb,
[Browsable(true)]
ccc,
}
public class testObject {
public xxx temp;
public xxx test {
get { return temp; }
set { temp = value; }
}
如何在运行时更改可浏览属性
例如,当按下btn1时,我想为all将browsable属性设置为false,如下所示:
private void button1_Click(object sender, RoutedEventArgs e)
{
object[] browsable;
Type type = typeof(xxx);
FieldInfo[] fieldInfos = type.GetFields();
foreach (FieldInfo fieldInfo in fieldInfos)
{
browsable = fieldInfo.GetCustomAttributes(typeof(BrowsableAttribute), false);
if (browsable.Length == 1)
{
BrowsableAttribute brAttr = (BrowsableAttribute)browsable[0];
fieldInfo.SetValue(brAttr, false);
}
}
}
但它会导致错误。您可以通过这种方式更改可浏览属性
object[] browsable;
Type type = typeof(xxx);
FieldInfo[] fieldInfos = type.GetFields();
foreach (FieldInfo fieldInfo in fieldInfos)
{
browsable = fieldInfo.GetCustomAttributes(typeof(BrowsableAttribute), false);
if (browsable.Length == 1)
{
System.ComponentModel.PropertyDescriptorCollection pdc = System.ComponentModel.TypeDescriptor.GetProperties(fieldInfo);
//Get property descriptor for current property
System.ComponentModel.PropertyDescriptor descriptor = pdc[24];// custom attribute
BrowsableAttribute attrib =
(BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo isReadOnly =
attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
isReadOnly.SetValue(attrib, true);
}
}
试试这个,这可能会帮你……什么错误?!?请发布完整准确的错误消息,因为我们真的看不到你的屏幕,也看不懂你的心思…好的。没错。但我有一个问题。似乎已完成,但未设置可浏览属性。需要更新吗?还是刷新?