如何在程序运行中动态改变对象属性在PropertyGrid可见性和只读属性

以下两个方法分别实现可见性和只读属性的动态改变:

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)


本文来自优快云博客,转载请标明出处:http://blog.youkuaiyun.com/departure19841030/archive/2009/10/24/4720452.aspx

<think>好的,我需要帮用户解决如何动态且批量修改单据中某列属性值的问题。首先,用户提到的引用链接是关于属性网格(PropertyGrid)的使用,尤其是在.NET框架中的应用。根据这个线索,我想到用户可能在使用类似C#WinForms的技术栈,因为PropertyGrid常用于这些环境。 接下来,用户需要的是动态且批量修改单据中的列属性。这里的“单据”可能指数据库中的记录或某种文档结构,比如Excel或者自定义格式的文件。需要明确用户的数据存储方式,但根据引用内容,假设是在使用某种对象模型,通过PropertyGrid来展示编辑属性动态修改属性通常涉及反射(Reflection),因为反射允许在运行时检查修改对象属性方法。批量处理的话,可能需要遍历多个单据对象,并对每个对象的特定属性进行更新。此外,用户可能需要批量应用相同的修改到多个属性,或者根据条件动态选择要修改的属性。 考虑到代码实现,可能需要以下步骤: 1. 获取需要修改的单据对象列表。 2. 确定目标属性新的属性值。 3. 使用反射或特定API来批量更新这些属性。 4. 如果是数据库操作,可能需要使用ORM工具或直接执行更新语句。 5. 在UI层面,比如PropertyGrid,可能需要刷新显示以确保修改后的值可见。 另外,用户提到的引用链接中的PropertyGrid部分,可能涉及如何通过代码动态设置属性描述、显示名称等属性特性,这可以通过自定义TypeDescriptor或使用属性类(如DisplayNameAttribute)来实现。例如,通过ICustomTypeDescriptor接口动态修改属性元数据,从而影响PropertyGrid中的显示行为。 需要注意的是,如果属性只读的或者有访问限制,可能需要特殊处理,比如调用特定方法或提升权限。同时,批量操作要考虑性能,避免频繁的反射调用影响效率,可以考虑缓存PropertyInfo对象或使用更高效的动态方法。 可能的解决方案包括: - 使用反射遍历对象列表,设置属性值。 - 在数据库中使用UPDATE语句批量修改字段。 - 在Excel等文档中使用VBA或Open XML SDK进行批量处理。 - 在PropertyGrid中通过自定义类型描述符动态调整属性。 需要根据用户的具体技术栈应用场景进一步细化。例如,如果使用Entity Framework,可以通过LINQ批量更新;如果使用ADO.NET,可以构造参数化SQL命令。 最后,验证解决方案的正确性效率,确保在动态修改时不会破坏数据完整性,并处理可能出现的异常情况,如属性不存在或值类型不匹配等。</think>### 动态批量修改单据列属性值的编程实现方法 #### 1. **基于反射的通用属性修改(面向对象场景)** 若单据以对象形式存在(如C#类实例),可通过反射动态修改属性: ```csharp public static void BatchUpdateProperty<T>(List<T> documents, string propertyName, object newValue) { PropertyInfo prop = typeof(T).GetProperty(propertyName); if (prop == null || !prop.CanWrite) return; foreach (var doc in documents) { // 类型安全转换(示例为字符串类型) if (newValue is string strValue) { prop.SetValue(doc, Convert.ChangeType(strValue, prop.PropertyType)); } // 添加其他类型判断... } } ``` *适用场景:内存对象集合的批量更新* #### 2. **数据库批量更新(SQL示例)** 若单据存储在关系型数据库中: ```sql -- 批量更新特定列 UPDATE Documents SET ColumnName = @NewValue WHERE FilterCondition; ``` *参数化示例(C# + Dapper)*: ```csharp connection.Execute( "UPDATE Documents SET Status = @Status WHERE Category = @Category", new { Status = "Approved", Category = "Finance" } ); ``` #### 3. **Excel文档批量处理(Open XML SDK)** 对于Excel单据的列修改: ```csharp using (SpreadsheetDocument doc = SpreadsheetDocument.Open("file.xlsx", true)) { WorksheetPart worksheetPart = doc.WorkbookPart.WorksheetParts.First(); foreach (Cell cell in worksheetPart.Worksheet.Descendants<Cell>()) { if (cell.CellReference.Value.StartsWith("D")) // 修改D列 { cell.CellValue = new CellValue("NewValue"); cell.DataType = CellValues.String; } } } ``` #### 4. **动态属性网格控制(WinForms PropertyGrid)** 若需要通过UI组件批量修改属性元数据: ```csharp // 自定义TypeDescriptionProvider public class DynamicPropertyDescriptor : CustomTypeDescriptor { public override PropertyDescriptorCollection GetProperties() { var baseProps = base.GetProperties(); var newProps = new PropertyDescriptorCollection(null); foreach (PropertyDescriptor pd in baseProps) { if (pd.Name == "TargetColumn") { // 动态修改属性特性 var attrs = new Attribute[] { new DisplayNameAttribute("动态列名"), new DefaultValueAttribute("默认值") }; newProps.Add(new ExtendedPropertyDescriptor(pd, attrs)); } } return newProps; } } // 应用描述符 TypeDescriptor.AddProvider(new DynamicTypeProvider(), typeof(Document)); ``` [^1] ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值