3

NB : This question is tagged C# too as it is a general question and an answer describing the use of this in C# is perfectly fine to me.

I have been exploring the PropertyGrid lately in the .Net Framework. I have checked on this property (BrowsableAttributes) and I have no idea of the use of it.

At first I thought this would be able to loop through every BrowsableAttribute in your SelectedObject then you would be able to find back the original property, that would have been usefull.

But no, apparently all this property does is giving you an AttributeCollection containing only BrowsableAttribute, all set to True...

Can someone enlighten me on what is the point for such method ? I don't even understand how it's usefull inside the .NET...

Dim attributes = MyPropertyGrid.BrowsableAttributes
For Each A As Attribute In attributes
    Dim Browsable As BrowsableAttribute = CType(A, BrowsableAttribute)
    'Then how can I use this ? it's only property is Browsable (True/False)
Next

I was originally trying to solve a problem, where I don't know which object is selected in the property grid but I want to gather the object's data.

I have no idea on what the object's type is because it comes from a dynamically loaded DLL. I only know it is a derived class of another one, which I know. But I'm interesting in backing up the Object's properties obtained from the Property Grid to be able to save and load them later on.

Since the property grid already contains all those values, I thought this kind of property could be a shorcut for writing more code. I don't want to use reflection to inspect the code while the Property Grid already did it.

Martin Verjans
  • 4,675
  • 1
  • 21
  • 48
  • 1
    Why do you *want* to use it? Are you trying to solve some problem (which one?) and think what this property can be used or is it academic question "what is `RandomClass.RandomProperty` is useful for"? And the answer is somewhere in tutorials about `PropertyGrid` and in [msdn](https://msdn.microsoft.com/en-us/library/system.componentmodel.browsableattribute(v=vs.110).aspx). – Sinatr Aug 24 '16 at 12:16
  • @Sinatr I updated my question, and no the answer is not in tutorials, I spent some time already on those msdn pages to figure it out. – Martin Verjans Aug 24 '16 at 12:25
  • 1
    *"I don't know which object is selected in the property"* - and again [XY problem](http://meta.stackexchange.com/q/66377/299295). **Why** do you need to know this? To do what? `PropertyGrid.SelectedObject` is the object, you can use `is` or `GetType()` to determine type. – Sinatr Aug 24 '16 at 12:29
  • @Sinatr updated again. I can again explain why I need to explore an object from a DLL but that would ask me to explain my project entirely... I tried to narrow down the question to what was relevant... – Martin Verjans Aug 24 '16 at 12:41
  • `PropertyGrid` uses `TypeDescriptor` mechanism to get properties of `SelectedObject` using `TypeDescriptor.GetProperties(component, attributes)`. The method returns filtered properties of the passed object based on passed attributes. As mentioned in documentations *Only properties with attributes matching the values specified are displayed in the PropertyGrid. The default is an AttributeCollection containing only BrowsableAttribute.Yes.*. You can find more description and some links in the answer. – Reza Aghaei Aug 24 '16 at 19:39

2 Answers2

4

The PropertyGrid iterates over all properties of an object, using an approach similar to this answer. While it does it, it checks all attributes associated with that property and checks if it can find a match with the AttributeCollection (sample code, how this can be done is here). If it can find it, it will show up in the property grid, otherwise not.

The purpose is, that you can hide public properties to show up in a property grid by either mark it with [Browsable(false)] if you need a blacklist support. If you like a whitelist approach, you can define your own Attribute, apply it to all desired properties and set the BrowsableAttribute to a collection containing your own attribute only.

Community
  • 1
  • 1
Oliver
  • 43,366
  • 8
  • 94
  • 151
  • Thank you for your answer. I understand how PropertyGrid finds it, the problem is that ProeprtyGrid.BrowsableAttributes only contains BrowsableAttributes set to Yes, so how is this usefull at all ? – Martin Verjans Aug 24 '16 at 12:55
  • @SuperPeanut things are browsable by default; IIRC this one is handled a bit as a special case - but you can add **your own** filters to make it work differently – Marc Gravell Aug 24 '16 at 13:04
  • @SuperPeanut: The explanation is in my second paragraph. It defines which attribute has to be attached to a property to make it visible in the property grid. You can replace it, if you like a whitelist approach about which properties should show up in the grid. – Oliver Aug 24 '16 at 13:05
  • @Oliver Ok I think I get it now. Thanks you very much. I totally misunderstood this property. – Martin Verjans Aug 24 '16 at 13:27
3

The job of BrowsableAttributes property has been described in documentations clearly:

Only properties with attributes matching the values specified are displayed in the PropertyGrid. The default is an AttributeCollection containing only BrowsableAttribute.Yes.

But how does it work?

The .NET Framework has two mechanisms to find metadata of a type:

  • Reflection APIs
  • TypeDescriptor mechanism

The metadata which returns by reflection for a type is not extensible and cannot be modified after compilation of the type while the metadata which returns by TypeDescriptor can be changed using IExtenderProvider, ITypeDescriptorFilterService or ICustomTypeDescriptor.

For example this is the type description mechanism which enables the designer to add some design-time properties which are not actual properties of the object like Modifier, Locked or GenerateMember.

PropertyGrid uses TypeDescriptor mechanism to get properties this way:

var properties = TypeDescriptor.GetProperties(component, attributes);

Which relies on TypeDescriptor.GetProperties method which you can find filtering rules in remarks section of the document.

The component in above method is SelectedObject of PropertyGrid and attributes is BrowsableAttributes property and as described in documentations only properties with attributes matching the values specified are displayed in the PropertyGrid. Since by default based on conventions we expect properties having [Brawsable(false)] doesn't show in PropertyGrid, so the property contains a Browsable attribute with Yes value.

Reza Aghaei
  • 120,393
  • 18
  • 203
  • 398