Code inspection: Possible mistaken argument of type 'System.Type'
This inspection reports cases where an argument of type System.Type
is used in a type-checking method instead of an object whose type needs to be checked.
In the example below, the extension method IsInstanceOfType() expects an object and not a type of an object, which is not obvious at the first glance. As a result, the code will not work as expected if the intention is to check the compatibility of the two types passed in the parameters.
If the intention of your code is to check the compatibility of two objects, you may use the IsAssignableFrom()
extension method:
public bool IsTheSameType(Type type, Type type2)
{
return type.IsInstanceOfType(type2);
}
public bool IsTheSameType(Type type, Type type2)
{
return type.IsAssignableFrom(type2);
}
Last modified: 08 August 2024