日期:2014-05-18  浏览次数:20859 次

方法属性动态设置问题
[BrowsableAttribute(var)]
void Founction(var2)
{
}

怎样才能动态的实像[BrowsableAttribute(var)]中的var可以根据var2的值而不同。


------解决方案--------------------
元数据支持泛型就OK了。

------解决方案--------------------
private void SetPropertyVisible(object target, string propertyName, bool visible)
{
PropertyDescriptorCollection pdc = TypeDescriptor.GetProperties(target);
PropertyDescriptor property = pdc[propertyName];
if (property != null)
{

AttributeCollection ac = property.Attributes;

Attribute attr = (BrowsableAttribute)ac[typeof(BrowsableAttribute)];

Type attrType = attr.GetType();
FieldInfo fld = attrType.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic);
fld.SetValue(attr, visible);
}
}

不过你给方法设置Browsable属性干嘛呢?方法本来就不在PropertyGrid里显示。