private int aa = 0; [Category(""), Description(""), ReadOnlyAttribute(false), BrowsableAttribute(true)] public int a { get { return aa; } set { aa = value; if (aa == 1) SetPropertyVisability(this, "b", false); } } private int bb = 0; [Category(""), Description(""), ReadOnlyAttribute(false), BrowsableAttribute(true)] public int b { get { return bb; } set { bb = value; } }
[Category(""), Description(""), ReadOnlyAttribute(false), BrowsableAttribute(true)]
public int a
{
get { return aa; }
set
{
aa = value;
if (aa == 1)
SetPropertyVisability(this, "b", false);
}
} private int bb = 0;
[Category(""), Description(""), ReadOnlyAttribute(false), BrowsableAttribute(true)]
public int b
{
get { return bb; }
set { bb = value; }
}
刚刚启动时左右两个实例分别有两个属性显示,且属性值都为0
之后我只修改了左边实例的a属性值为1,所以左边的属性b不见了,同时右边实例的属性b也不见了
类对象,存储在不同的堆上,怎么可能级联修改呢!根据你的代码,我测试了一下,你可以看下效果
class MyType
{
private int aa = 0;
[Category(""), Description(""), ReadOnlyAttribute(false), BrowsableAttribute(true)]
public int a
{
get { return aa; }
set
{
aa = value;
if (aa == 1)
SetPropertyVisability(this, "b", false);
}
} private int bb = 0;
[Category(""), Description(""), ReadOnlyAttribute(false), BrowsableAttribute(true)]
public int b
{
get { return bb; }
set { bb = value; }
} /// <summary>设置属性的可见性</summary>
/// <param name="obj">属性所属的类</param>
/// <param name="propertyName">属性名</param>
/// <param name="visable">可见性</param>
public static void SetPropertyVisability(object obj, string propertyName, bool visable)
{
Type type = typeof(System.ComponentModel.BrowsableAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
fld.SetValue(attrs[type], visable);
}
}
MyType m1 = new MyType();
MyType m2 = new MyType();
m1.a = 1;
Console.WriteLine(m1.a + "," + m1.b);
Console.WriteLine(m2.a + "," + m2.b);