2021-04-28 16:49:39 +02:00
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.
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
== 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
{ /* ... */ }
----
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
== Compliant Solution
----
var s = "abc";
if (s.GetType().IsInstanceOfType("string"))
{ /* ... */ }
----
2021-04-28 18:08:03 +02:00
2021-04-28 16:49:39 +02:00
== Exceptions
----
typeof(Type).GetType(); // Can be used by convention to get an instance of ‘ System.RuntimeType’
----
2021-04-28 18:08:03 +02:00
2021-06-02 20:44:38 +02:00
2021-06-03 09:05:38 +02:00
ifdef::env-github,rspecator-view[]
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
include::comments-and-links.adoc[]
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]