Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove C# attribute of a property dynamically

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?

like image 795
SysAdmin Avatar asked Mar 20 '10 13:03

SysAdmin


People also ask

What is remove in C?

C library function - remove() The C library function int remove(const char *filename) deletes the given filename so that it is no longer accessible.

How do I remove a character from a string in C?

We have removeChar() method that will take a address of string array as an input and a character which you want to remove from a given string. Now in removeChar(char *str, char charToRemove) method our logic is written.

Is delete a keyword in C?

The delete keyword replaces the free function in C and will release storage reserved with new. int *ptr1; // Declare a pointer to int.

Is remove a system call?

remove isn't even a system call. May be This can be useful. Possible duplicate of How to remove a file in C program?


Video Answer


4 Answers

The CodingLight.com blog moved to blogspot (the above link is broken). See http://codinglight.blogspot.com/2008/10/changing-attribute-parameters-at.html.

Moreover, SysAdmin's followup did not mention the [RefreshProperties(RefreshProperties.All)] attribute that seems to be necessary for an actually-working solution.

Finally, I believe that even David Morton (author of the quoted article) missed one very important thing: if the class (ContactInfo, in SysAdmin's followup example) does not have at least one property with the [ReadOnly] attribute defined at compile time, then when the "isReadOnly" FieldInfo is set to true at runtime the result is that the whole class turns read-only.

like image 110
Mau Avatar answered Sep 21 '22 23:09

Mau


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:

http://codinglight.blogspot.com/2008/10/changing-attribute-parameters-at.html

Edit: fixed link

like image 33
andreialecu Avatar answered Sep 23 '22 23:09

andreialecu


I have to agree w/ Omu; you're really talking about two classes (view models) in this case, to support your two different views. Something like

CreateContactViewModel and EditContactViewModel

like image 45
Paul Avatar answered Sep 21 '22 23:09

Paul


it's not possible at the moment to remove attributes dinamycally (at runtime)

as a suggestion you can do 2 classes: one with the attributes and one without

like image 45
Omu Avatar answered Sep 21 '22 23:09

Omu