如何在程序运行中动态改变对象属性在PropertyGrid可见性和只读属性
来源:互联网 发布:风云排名知乎 编辑:程序博客网 时间:2024/05/01 12:44
以下两个方法分别实现可见性和只读属性的动态改变:
void SetPropertyVisibility(object obj, string propertyName, bool visible)
{
Type type = typeof(BrowsableAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("browsable", BindingFlags.Instance | BindingFlags.NonPublic);
fld.SetValue(attrs[type], visible);
}
void SetPropertyReadOnly(object obj, string propertyName, bool readOnly)
{
Type type = typeof(System.ComponentModel.ReadOnlyAttribute);
PropertyDescriptorCollection props = TypeDescriptor.GetProperties(obj);
AttributeCollection attrs = props[propertyName].Attributes;
FieldInfo fld = type.GetField("isReadOnly", BindingFlags.Instance | BindingFlags.NonPublic | BindingFlags.CreateInstance);
fld.SetValue(attrs[type], readOnly);
}
'上面是cs的代码 以下是自己改的vb代码 测试通过
Function SetPropertyVisibility(ByVal obj As Object, ByVal propertyName As String, ByVal visible As Boolean)
Dim mytype As Type = GetType(BrowsableAttribute)
Dim props As PropertyDescriptorCollection = TypeDescriptor.GetProperties(obj) '得到一个属性集
Dim attrs As AttributeCollection = props(propertyName).Attributes
Dim fld As FieldInfo = mytype.GetField("browsable", BindingFlags.Instance Or BindingFlags.NonPublic)
fld.SetValue(attrs(mytype), visible)
Return Nothing
End Function
'同一个类中调用的话 如下就OK了
SetPropertyVisibility(Me, "属性名称", True)
本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/departure19841030/archive/2009/10/24/4720452.aspx
- 如何在程序运行中动态改变对象属性在PropertyGrid可见性和只读属性
- 如何在程序运行中动态改变对象属性在PropertyGrid可见性和只读属性
- 用反射实现PropertyGrid中的属性动态实现只读、可见
- C# 设计时动态改变实体在PropertyGrid中显示出来的属性
- 在VB程序中,通过代码改变Combo控件只读属性Style值
- 在VB程序中,通过代码改变Combo控件只读属性Style值
- propertygrid在其属性面板中加载json对象带函数的属性或者事件
- 在程序中去掉EXCEL的只读属性
- 定义类的可见属性,并定义其在propertyGrid中所调用的设计器的实现
- 在VC中,如何修改文件的只读属性设置
- 在oc中如何修改控件的一些只读属性
- 让Vector3属性在PropertyGrid空间中显示的Vector3Converter
- C# 自定义属性在propertyGrid控件中显示
- 实现PointF和SizeF的属性在PropertyGrid中可编辑的方法
- 如何在SpringMVC框架中利用Java反射机制和Javassist实现Java对象、属性、注解的动态创建生成
- 运行时设定对象属性只读
- 在VC中,如何修改文件的只读属性设置(转)
- 在VC中,如何修改文件的只读属性设置(转)
- hdu 4312 Meeting point-2
- jBPM4的PVM解析
- 用内存映射文件在进程中共享数据
- 让数据库飞起来 10大DB2优化技巧
- 优化SQL Server数据库查询方法
- 如何在程序运行中动态改变对象属性在PropertyGrid可见性和只读属性
- 属性串行化
- Peas Malaysian show goes ahead 黑眼豆豆乐队马来西
- Yii源码分析——CComponent
- 《编程之道》
- 值得关注的三个开源的云计算项目
- 实例化Instancing
- MES系列--(2)JNA使用介绍
- dynamic增强C#泛型机制