我有一类的一组属性如下给出。
I have a class with a set of properties As given below.
class ContactInfo { [ReadOnly(true)] [Category("Contact Info")] public string Mobile { get; set; } [Category("Contact Info")] public string Name{ get; set; } }
这个类的对象被分配到一个属性网格,从而使用户能够更新现有联系人。你可以看到,手机被标记为只读。
The objects of this class is being assigned to a property grid, so that the users can update an existing contact. you can see that Mobile is marked as ReadOnly.
但是,当我想添加一个全新的联系,我希望用户能够编辑联系人手机也。为此,我需要的从类型中删除只读属性动态,对象分配到属性网格之前。这可能吗?
But, when I want to add an entirely new Contact, I would want the users to be able to edit the contact Mobile also. For that I need to remove the Readonly property dynamically from the Type, before assigning the object to the property grid. Is it possible?
推荐答案您不能删除在运行时的属性,但您可以使用反射到只读属性的只读私人支持字段更改为False。使得它相当于[只读(假)]
You can not remove the attribute at runtime, but you can use reflection to change the ReadOnly attribute's ReadOnly private backing field to False. Making it the equivalent of [ReadOnly(false)]
请参阅本文的详细信息:
See this article for details:
codinglight.blogspot/2008/ 10 /更改属性参数,at.html
编辑:固定链接
更多推荐
动态删除属性的C#属性
发布评论