Wednesday, 8 December 2010

EditorBrowsable

The EditorBrowsableAttribute class specifies whether a property or method is viewable via Intellisense.
It is used like this:

using System.ComponentModel;
...
[EditorBrowsableAttribute(EditorBrowsableState.Advanced)]
protected void AdvancedMethod()
{
//The property or method is a feature that only advanced users should see. An editor can either show or hide such properties
}
[EditorBrowsableAttribute(EditorBrowsableState.Always)]
protected void AlwaysMethod()
{
//The property or method is always browsable from within an editor
}
[EditorBrowsableAttribute(EditorBrowsableState.Never)]
protected void NeverMethod()
{
//The property or method is never browsable from within an editor
}
I wanted to use it with a class that provided a fluent interface to hide the common methods like Equals, GetHashCode, GetType, ToString etc. to make the fluent interface syntax cleaner.
It transpires that it is only used by Intellisense when the decorated item is in a referenced assembly - shame.
Here is the fuller explanation from Linda Lui, Microsoft Online Community Support back in June 2006:

Speaking for C#, the EditorBrowsableAttribute attribute only influences C#
IntelliSense filtering behavior if the decorated item is imported from
metadata.
That's to say, you should compile the EditorBrowsableAttribute-decorated
project(e.g project 1) into an assembly(.dll or .exe) and then in another
project(e.g project 2) add a reference to that assembly. In project 2, you
should see the attribute at work.
C# IntelliSense filtering behavior is not influenced if you are coding in
project 1. What's more, if you add a project-to-project reference to
project 1 in project 2, C# IntelliSense filtering behavior is not
influenced when you are coding in project 2 either.
The IDE is intended to filter items from consumers of your assembly and not
to filter items from yourself as you code the assembly.

No comments: