Why EditorBrowsableState.Never exists?

2019-06-21 23:36发布

问题:

There exists an EditorBrowsableAttribute in the System.ComponentModel namespace. The type EditorBrowsableState has following members:

  • Advanced
  • Always
  • Never

What could be the reason for this annoying attribute? Especially the state Never. It's hiding functionality and then when you find it you feel dirty using it. So if you are working at Microsoft and implemented this class tell me plz... :)

The only case I can imagine this attribute makes sense is when you want to mark something as obsolete and hide it from intellisense to focus better. But in all cases I encountered this attribute was used without an obsolete attribute. The most recent example i found is in wpf the Dispatcher.CheckAccess() method.

回答1:

IMO is an attribute devoted to libraries building, inside your VS solution (when dependencies are not external) Intellisense will simply ignore it. There are few useful use-cases.

Let's first see EditorBrowsableState.Advanced: with some languages (notably VB.NET) you can instruct IDE (actually Intellisense) to hide advanced members so you'll see only most common methods. Less noise for very beginners (you can enable full view when you master BCL little bit better and you're not intimated by huge number of obscure methods).

EditorBrowsableState.Never has other usages.

  • One is what you said (hide deprecated obsolete methods). An example is old Form.Closing event (hidden because replaced and extended by Form.FormClosing).
  • Another one is to hide errors and typos. If that attribute was applicable to, for example, Keys.HanguelMode then typo won't be visible (but present for compatibility).
  • It may also be useful if you need to expose few safe internal methods to other assemblies. You won't pollute your library interface with that methods (you may keep them documented and keep them as internal use only). It may be better than exposing all internal details through friend assemblies with InternalsVisibleToAttribute.
  • You may use it to hide potentially dangerous methods. You provide them because someone may need them but you want to stop people to use them, if possible.


回答2:

EditorBrowsableState attribute is primarily meant for use in Visual Designers. EditorBrowsableState.Never is appropriate where some properties of a UI Widget or Custom Control being used in the designer surface is not relevant to or appropriate for the visual design aspects.