0

Am having a class as following,

class Person { private string _PersonName; [Category("General")] [ReadOnly(true)] [Browsable(true)] public string Name { get { return _PersonName; } set { _PersonName= value; } } } 

and an ObservableCollection of Person objects as following,

ObservableCollection<Person> Persons = new ObservableCollection<Person>(); 

Consider, I am having 5 Person objects in Persons(ObservableCollection).

Person Person1 = new Person(){Name = "User 1";} Person Person2 = new Person(){Name = "User 2";} Person Person3 = new Person(){Name = "User 3";} Person Person4 = new Person(){Name = "User 4";} Person Person5 = new Person(){Name = "User 5";} Persons.Add(Person1); Persons.Add(Person2); Persons.Add(Person3); Persons.Add(Person4); Persons.Add(Person5); 

Now, am changing Browsable attribute value of Second object from true to false dynamically as follows,

PropertyDescriptor pd = TypeDescriptor.GetProperties(Person1)["Name"]; BrowsableAttribute ba =(BrowsableAttribute)pd.Attributes[typeof(BrowsableAttribute)]; FieldInfo IsBrowsable = ba.GetType().GetField("browsable", BindingFlags.IgnoreCase | BindingFlags.Instance | BindingFlags.NonPublic); IsBrowsable.SetValue(ba, false); 

after this change, value of Browsable(true) attribute of all other objects becomes false.

This is the problem, How to prevent this?

Thank you all.

9
  • Persons.Add(new Person().Name = "User5"); doesn't look right at all. Instead try Persons.Add(new Person() { Name = "User5" }); Commented Aug 11, 2015 at 9:35
  • What makes you think you are "changing Browsable attribute value of Second object"? There is no Person instance involved in your code. Commented Aug 11, 2015 at 9:35
  • AFAIK, attributes are applied at the aspect level (e.g. Class, Method, Property, etc) and not at the 'object' level. When you change the attribute value, it's applied for the Name property of Person class (which means for all instances/objects of Person). See 'How do attribute classes work'? Commented Aug 11, 2015 at 9:53
  • @publicgk Thank you very much for this clarification. Is there any way to achieve this scenario? Commented Aug 11, 2015 at 10:06
  • What exactly do you want to achieve? Commented Aug 11, 2015 at 10:08

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.