rspec/rules/S3443/csharp/rule.adoc

39 lines
944 B
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

If you call ``++GetType()++`` on a ``++Type++`` variable, the return value will always be ``++typeof(System.Type)++``. So there's no real point in making that call. The same applies to passing a type argument to ``++IsInstanceOfType++``. In both cases the results are entirely predictable.
== Noncompliant Code Example
----
var intType = typeof(int);
var runtimeType = intType.GetType(); // Noncompliant, always typeof(System.RuntimeType)
var s = "abc";
if (s.GetType().IsInstanceOfType(typeof(string))) // Noncompliant; false
{ /* ... */ }
----
== Compliant Solution
----
var s = "abc";
if (s.GetType().IsInstanceOfType("string"))
{ /* ... */ }
----
== Exceptions
----
typeof(Type).GetType(); // Can be used by convention to get an instance of System.RuntimeType
----
ifdef::env-github,rspecator-view[]
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
endif::env-github,rspecator-view[]