Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hide Equals and ReferenceEquals

I am creating an internal class to provide a construct for the Contract pattern.

using System;
using System.Runtime.Serialization;

namespace DCS2000.Common35.Core
{
  public class Assertion
  {
    public static void Ensure(bool test)
    {
        if (!test)
        {
            throw new PreconditionException("Precondition test failed");
        }
    }

    public static void Ensure(object obj)
    {
        if (obj == null)
        {
            throw new PreconditionException("Precondition null object failed");
        }
    }

    public static void Require(bool test)
    {
        if (!test)
        {
            throw new PostconditionException("Postcondition test failed");
        }
    }

    public static void Require(object obj)
    {
        if (obj == null)
        {
            throw new PostconditionException("Postcondition null object failed");
        }
    }
  }
}

When a developer goes to use this they will see these as options in Intellisense:

  • Ensure
  • Equals
  • ReferenceEquals
  • Require

This is confusing and I am wondering if there is a way to hide Equals and ReferenceEquals.

NOTE: I have already tried this, but it did not work for me:

    [EditorBrowsable(EditorBrowsableState.Never)]
    public override bool Equals(object obj)
    {
        return base.Equals(obj);
    }
like image 758
Lucas B Avatar asked Aug 03 '10 14:08

Lucas B


2 Answers

To add to Matti's answer, EditorBrowsableState.Never depends on the user's Visual Studio settings under Options, Text Editor, C#, General.

It only takes effect if the user has 'Hide advanced members' turned on. Visual Studio defaults to showing all members.

like image 192
Tim Robinson Avatar answered Sep 19 '22 19:09

Tim Robinson


Use:

    [EditorBrowsable(EditorBrowsableState.Never)]
    public override bool Equals(object obj)
    {
        throw new Exception("Assertion does not implement Equals, use Ensure or Require");
    }

    [EditorBrowsable(EditorBrowsableState.Never)]
    public new bool ReferenceEquals(object objA, object objB)
    {
        throw new Exception("Assertion does not implement ReferenceEquals, use Ensure or Require");
    }

This will hide the members if the developer has appropriate VS settings set, and will immediately notify, alas during run-time, the developer that they are inappropriately using Equals or ReferenceEquals, should they inadvertently use it in the code base.

like image 40
Lucas B Avatar answered Sep 19 '22 19:09

Lucas B