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:
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);
}
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With