Skip to content

Gendarme.Rules.BadPractice.DoNotUseGetInterfaceToCheckAssignabilityRule(2.10)

Sebastien Pouliot edited this page Jan 22, 2011 · 2 revisions

DoNotUseGetInterfaceToCheckAssignabilityRule

Assembly: Gendarme.Rules.BadPractice
Version: 2.10

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
Clone this wiki locally