Skip to content

Gendarme.Rules.Design.Linq.AvoidExtensionMethodOnSystemObjectRule(git)

Sebastien Pouliot edited this page Mar 2, 2011 · 1 revision

AvoidExtensionMethodOnSystemObjectRule

Assembly: Gendarme.Rules.Design.Linq
Version: git

Description

Extension methods should not be used to extend System.Object. Such extension methods cannot be consumed by some languages, like VB.NET, which use late-binding on System.Object instances.

Examples

Bad example:

public static class Extensions {
    public static string ToDebugString (this object self)
    {
        return String.Format ("'{0}', type '{1}', hashcode: {2}",
        self.ToString (), self.GetType (), self.GetHashCode ());
    }
}

Good example:

public static class Extensions {
    public static string ToDebugString (this DateTime self)
    {
        return String.Format ("'{0}', type '{1}', hashcode: {2}",
        self.ToString (), self.GetType (), self.GetHashCode ());
    }
}

Notes

  • This rule is available since Gendarme 2.2

Source code

You can browse the latest source code of this rule on github.com

Clone this wiki locally