首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > C# >

方法属性动态设置有关问题

2012-03-15 
方法属性动态设置问题[BrowsableAttribute(var)]void Founction(var2){}怎样才能动态的实像[BrowsableAttr

方法属性动态设置问题
[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里显示。

热点排行