Code inspection: Possible mistaken 'GetType()' invocation
This inspection reports cases where GetType()
is used in an argument of a type-checking method that expects an object whose type needs to be checked.
In the example below, the extension method IsInstanceOfType() expects an object and not its type, which is not obvious at the first glance. As a result, the code will not work as expected. The compiler issues no warnings or errors in this case because the System.Type
returned by GetType()
is compatible with Object
.
public bool IsOfType(Type type, object value)
{
return type.IsInstanceOfType(value.GetType());
}
public bool IsOfType(Type type, object value)
{
return type.IsInstanceOfType(value);
}
Last modified: 30 July 2024