动态删除属性的C#属性

编程入门 行业动态 更新时间:2024-10-26 02:33:26
本文介绍了动态删除属性的C#属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一类的一组属性如下给出。

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#属性

本文发布于:2023-11-07 17:20:48,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1567017.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   动态

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!