53 lines
1.1 KiB
Plaintext
53 lines
1.1 KiB
Plaintext
== Why is this an issue?
|
||
|
||
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
|
||
|
||
[source,csharp]
|
||
----
|
||
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
|
||
|
||
[source,csharp]
|
||
----
|
||
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[]
|
||
|
||
'''
|
||
== Implementation Specification
|
||
(visible only on this page)
|
||
|
||
include::message.adoc[]
|
||
|
||
include::highlighting.adoc[]
|
||
|
||
'''
|
||
== Comments And Links
|
||
(visible only on this page)
|
||
|
||
include::comments-and-links.adoc[]
|
||
endif::env-github,rspecator-view[]
|