Skip to content

Gendarme.Rules.BadPractice.DoNotUseGetInterfaceToCheckAssignabilityRule(git)

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

DoNotUseGetInterfaceToCheckAssignabilityRule

Assembly: Gendarme.Rules.BadPractice
Version: git

Description

This rule checks for calls to Type.GetInterface that look like they query if a type is supported, i.e. the result is only used to compare against null. The problem is that only assembly qualified names uniquely identify a type so if you just use the interface name or even just the name and namespace you may get unexpected results.

Examples

Bad example:

if (type.GetInterface ("IConvertible") != null)  {
    // then the type can be assigned to IConvertible
    // but what if there is another IConvertible in there ?!?
}

Good example:

if (typeof (IConvertible).IsAssignableFrom (type))  {
    // then the type can be assigned to IConvertible
    // without a doubt!
}

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