12

The IHideObjectMembers trick (a.k.a IFluentInterface) can be used e.g. in fluent interface implementations to hide System.Object members from IntelliSense. (If you don't know this trick, you can read up on it via the above link; I'm just repeating the interface's usual declaration here:)

using System;
using System.ComponentModel;

[EditorBrowsable(EditorBrowsableState.Never)]
public interface IHideObjectMembers
{
    [EditorBrowsable(EditorBrowsableState.Never)] bool Equals(object obj);
    [EditorBrowsable(EditorBrowsableState.Never)] int GetHashCode();
    [EditorBrowsable(EditorBrowsableState.Never)] Type GetType();
    [EditorBrowsable(EditorBrowsableState.Never)] string ToString();
}

I'm now supposed to be able to hide System.Object members on another type as follows:

public class SomeClass : IHideObjectMembers { ... }

or:

public interface ISomeInterface : IHideObjectMembers { ... }

I tried this in both VS 2008 Express and VS 2008 Standard. However, no members are hidden from IntelliSense at all. I have used the EditorBrowsableAttribute in different projects and it always worked well; however, it doesn't work in this particular scenario.

If things had worked as expected, I would only have seen the SomeMethodTwo method.

Am I missing something?


P.S.: You can infer my example code from the declarations and the screenshot. I have a class SomeClass with a single dummy method called SomeMethodTwo. Very simple. I have not re-implemented the four System.Object methods in this class, as this should not be necessary.

Community
  • 1
  • 1
stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268

3 Answers3

12

Before a working solution was posted (see above), I researched and experimented a bit, and found out this:

  • IntelliSense needs to be told to ignore properties marked with an EditorBrowsableState equalling Advanced or Never. This is achieved in Visual Studio via the menu item ToolsOptions...Text editorC#IntelliSenseHide advanced members.

  • The IHideObjectMembers trick works only from "other" assemblies, and only when the project that makes use of IHideObjectMembers is not loaded in the same solution.

  • Hiding non-static members of object sometimes doesn't work when done via a IHideObjectMembers interface, but it works when the methods are explicitly overridden directly in the class... which unfortunately makes the shown hiding technique less useful.

Sources of these clues:

stakx - no longer contributing
  • 83,039
  • 20
  • 168
  • 268
  • Interesting. I haven't been able to make it work outside or inside of the solution using VS 2010. – Sergey Akopov Mar 24 '11 at 04:30
  • 1
    +1 can believe this is only the second ever upvote on your work. (Trying to diagnose same on VS10) – Ruben Bartelink Jun 08 '11 at 14:47
  • 1
    NB wasnt a VS10 issue - all it changes is that thers no Hide advanced members setting to check. It works as long as its not a project reference. One negative I've noticed is that Ctrl., implement interface explicitly now generates the 4 methods (tho it only offers itself if a 'real method' is missing) but I guess life will go on. (Same negative applies in VS08) – Ruben Bartelink Jun 08 '11 at 15:24
7

The trick works if you don't use

var x = new SomeClass();

but explicitely use the interface that inherits from IHideObjectMembers

ISomeInterface x = new SomeClass();

var takes the Type of the concrete class. Thus IntelliSense will look for object.ToString() and not IHideObjectMembers.ToString(). The former is not decorated with the EditorBrowsableAttribute while the later is.

Daniel Cazzulino explicitely refers to interfaces in his post

we’ve done this with all the interfaces in our fluent API

Sebastian Weber
  • 6,766
  • 2
  • 30
  • 49
4

This trick only works when you are using types that are included in referenced DLLs. These referenced assemblies cannot be Visual Studio project references. If you reference the DLL themselves, then the trick should work for you.

Julian Dominguez
  • 2,573
  • 21
  • 15