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:

A much cleaner intellisense is possible though:

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 :)
/kzu
/kzu dev↻d