2023-05-03 11:06:20 +02:00
== Why is this an issue?
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
2023-05-03 11:06:20 +02:00
=== Noncompliant code example
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
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
2023-05-03 11:06:20 +02:00
=== Compliant solution
2021-04-28 16:49:39 +02:00
2022-02-04 17:28:24 +01:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
var s = "abc";
if (s.GetType().IsInstanceOfType("string"))
{ /* ... */ }
----
2021-04-28 18:08:03 +02:00
2023-05-03 11:06:20 +02:00
=== Exceptions
2021-04-28 16:49:39 +02:00
2023-05-25 14:18:12 +02:00
[source,csharp]
2021-04-28 16:49:39 +02:00
----
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-09-20 15:38:42 +02:00
'''
== Implementation Specification
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== Message
* Remove this use of "GetType" on a "System.Type".
* Pass an argument that is not a "System.Type" or use "IsAssignableFrom".
* Remove the "GetType" call, it's suspicious in an "IsInstanceOfType" call.
=== Highlighting
* ``++.GetType()++``
* argument to ``++IsInstanceOfType++``
2021-09-20 15:38:42 +02:00
2021-06-08 15:52:13 +02:00
'''
2021-06-02 20:44:38 +02:00
== Comments And Links
(visible only on this page)
2023-05-25 14:18:12 +02:00
=== on 8 Dec 2015, 09:00:32 Tamas Vajk wrote:
\[~ann.campbell.2] I made some changes (description/title), could you run through them? Thanks
2021-06-03 09:05:38 +02:00
endif::env-github,rspecator-view[]