Daniel Cazzulino's Blog : How to hide System.Object members from your interfaces

Subscriptions

<May 2008>
SuMoTuWeThFrSa
27282930123
45678910
11121314151617
18192021222324
25262728293031
1234567

  Microsoft MVP Profile

News

 

 Contact

Post Categories

How to hide System.Object members from your interfaces

Sometimes, System.Object methods (Equals, GetHashCode, GetType and ToString) only contribute clutter to VS intellisense. Everyone knows those members are always there, yet they are seldom used explicitly. This is especially important (and annoying) for fluent APIs that define the flow of invocations in terms of interfaces and usually have few members at each "step" of the statement.

For example, in the following Moq expectation, at the particular step in the statement, there are only two "real" invocations that make sense. However, they are obscured by the System.Object members, which even outnumber them:

image

 

A much cleaner intellisense is possible though:

image 

 

The trick comes from the System.ComponentModel.EditorBrowsableAttribute, which controls visibility of members in VS intellisense. To hide a member from intellisense, you apply the following attribute to it:

[EditorBrowsable(EditorBrowsableState.Never)]

Now, you don't want to have to override all four object members in every type just to apply the attribute. A quite elegant solution exists, which involves taking advantage of implicit interface implementation. In particular, you can define an interface that re-defines all object members and applies the attribute:

    [EditorBrowsable(EditorBrowsableState.Never)]
    public interface IHideObjectMembers
    {
        [EditorBrowsable(EditorBrowsableState.Never)]
        Type GetType();

        [EditorBrowsable(EditorBrowsableState.Never)]
        int GetHashCode();

        [EditorBrowsable(EditorBrowsableState.Never)]
        string ToString();

        [EditorBrowsable(EditorBrowsableState.Never)]
        bool Equals(object obj);
    }

Now you simply add this interface to all your classes or interfaces where you want to hide these members. Starting in Moq v2, we've done this with all the interfaces in our fluent API so that they don't clutter your discovery of the expected flow:

public interface IVerifies : IHideObjectMembers

 

Sometimes I do love VS :)

posted on Monday, March 10, 2008 9:59 AM by kzu

# How to hide System.Object members from your interfaces @ Monday, March 10, 2008 10:04 AM

Sometimes, System.Object methods ( Equals , GetHashCode , GetType and ToString ) only contribute clutter

Anonymous

# Interesting Finds: 2008.03.13 @ Wednesday, March 12, 2008 7:17 PM

Other:ImdbServices-searchmoviesbycode,title,actors,directors,etconimdb.comWeb:ProtoFlow...

Anonymous

# Interesting Finds: 2008.03.13 @ Wednesday, March 12, 2008 7:18 PM

Other: Imdb Services - search movies by code,title,actors,directors,etc on imdb.com Web: ProtoFlow: Coverflow

Anonymous

# Hiding Members in Intellisense @ Sunday, March 16, 2008 9:53 AM

Hiding Members in Intellisense

Anonymous

# How to hide System.Object members from your interfaces @ Wednesday, March 26, 2008 4:03 AM

You've been kicked (a good thing) - Trackback from DotNetKicks.com

Anonymous