如何在程序运行中动态改变对象属性在PropertyGrid可见性和只读属性
2012-07-30 11:23
375 查看
以下两个方法分别实现可见性和只读属性的动态改变:
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
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可见性和只读属性
- C#中的PropertyGrid绑定对象,通过改变某一值而动态设置部分属性的特性
- 用反射实现PropertyGrid中的属性动态实现只读、可见
- C#如何访问水晶报表中的对象,以及动态改变对象的内容?
- Flex中如何通过rowCount属性,动态改变DataGrid显示行数
- JavaScript 如何给js 空对象动态添加未知属性和值?
- 在VB程序中,通过代码改变Combo控件只读属性Style值
- 如何动态改变input type=submit 的type属性为button
- 如何查看一个运行的exe执行程序需要有哪些DLL动态链接库
- JavaScript 如何给js 空对象动态添加未知属性和值?
- Yii:如何动态改变CDataColumn的htmlOptions属性
- 如何在SpringMVC框架中利用Java反射机制和Javassist实现Java对象、属性、注解的动态创建生成
- C# 设计时动态改变实体在PropertyGrid中显示出来的属性
- java程序运行时对象是怎么进行放置安排? 内存是如何分配的呢?
- objective-c运行时机制runtime,动态获取对象的属性和成员变量
- c# 对象运行时动态添加属性、方法
- 对象不改变只改变对象的属性多线程运行结果还是同步
- 关于GetActiveObject,如何判断所取得对象的类型、、、可以用来检测某个程序是否运行
- (论坛答疑点滴)如何对动态的对象进行操作,属性付值
- C# 设计时动态改变实体在PropertyGrid中显示出来的属性