我正在尝试在运行时更改一个类中变量的Browsable属性。包含该属性的类如下:
public class DisplayWallType : WallType
{
[TypeConverter(typeof(SheathingOptionsConverter))]
[Browsable(false)]
public override Sheathing SheathingType { get; set; }
public DisplayWallType(string passedName, string passedType, bool passedMirrorable, Distance passedHeight, string passedStudPattern, Distance passedStudSpacing, VaporBarrier passedBarrier)
: base(passedName, passedType, passedMirrorable, passedHeight, passedStudPattern, passedStudSpacing, passedBarrier)
{
}
/// <summary>
/// Empty constructor for XML serialization
/// </summary>
public DisplayWallType()
{
}
}
我最初将SheathingType设置为false,因为我不希望该属性显示在我的应用程序的第一个表单中。然而,在我的第二个表格中,我希望它可见,因此我有了这个方法来更改它
private void _makeSheathingVisible(DisplayWallType wallTypeToMakeSheathingVisible)
{
PropertyDescriptor descriptor = TypeDescriptor.GetProperties(wallTypeToMakeSheathingVisible.GetType())["SheathingType"];
BrowsableAttribute attrib = (BrowsableAttribute)descriptor.Attributes[typeof(BrowsableAttribute)];
FieldInfo isBrow = attrib.GetType().GetField("browsable", BindingFlags.NonPublic | BindingFlags.Instance);
isBrow.SetValue(attrib, true);
}
那个方法以上接受一个DisplayWallType对象并确保Browsable被设置为true。我的第二个表单是一个树形视图和属性网格的组合。树形视图用DisplayWallType实例填充,当选中一个时,它会传递到该方法中,以便SheathingType显示在PropertiesGrid中,并与父类的其余属性一起显示。