2023-05-03 11:06:20 +02:00
== Why is this an issue?
2023-06-07 15:41:34 +02:00
Calling `GetType` on a `Type` variable will always return the `System.Type` representation, which is equivalent to `typeof(System.Type)`. This also applies to passing a `Type` argument to `IsInstanceOfType` which always returns `false`.
2021-04-28 16:49:39 +02:00
2023-06-01 09:23:16 +02:00
In both cases, the results are entirely predictable and should be avoided.
2021-04-28 18:08:03 +02:00
2023-06-01 09:23:16 +02:00
=== Exceptions
Calling `GetType` on `System.Type` is considered compliant to get an instance of `System.RuntimeType`, as demonstrated in the following 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
----
2023-06-01 09:23:16 +02:00
typeof(Type).GetType(); // Can be used by convention to get an instance of 'System.RuntimeType'
2021-04-28 16:49:39 +02:00
----
2023-06-01 09:23:16 +02:00
== How to fix it
2021-04-28 18:08:03 +02:00
2023-06-07 15:41:34 +02:00
Make sure the usage of `GetType` or `IsInstanceOfType` is invoked with the correct variable or argument type.
2021-04-28 16:49:39 +02:00
2023-06-01 09:23:16 +02:00
=== Code examples
2021-04-28 16:49:39 +02:00
2023-06-01 09:23:16 +02:00
==== Noncompliant code example
2021-04-28 16:49:39 +02:00
2023-06-01 09:23:16 +02:00
[source,csharp,diff-id=1,diff-type=noncompliant]
----
void ExamineSystemType(string str)
{
Type stringType = str.GetType();
Type runtimeType = stringType.GetType(); // Noncompliant
if (stringType.IsInstanceOfType(typeof(string))) // Noncompliant; will always return false
{ /* ... */ }
}
----
2021-04-28 18:08:03 +02:00
2023-06-01 09:23:16 +02:00
==== Compliant solution
2021-04-28 16:49:39 +02:00
2023-06-01 09:23:16 +02:00
[source,csharp,diff-id=1,diff-type=compliant]
2021-04-28 16:49:39 +02:00
----
2023-06-01 09:23:16 +02:00
void ExamineSystemType(string str)
{
Type stringType = str.GetType();
if (stringType.IsInstanceOfType(str)) // Compliant
{ /* ... */ }
}
2021-04-28 16:49:39 +02:00
----
2021-04-28 18:08:03 +02:00
2023-06-01 09:23:16 +02:00
== Resources
=== Documentation
* https://learn.microsoft.com/en-us/dotnet/api/system.type["Type" class]
* https://learn.microsoft.com/en-us/dotnet/api/system.object.gettype["GetType" Method]
* https://learn.microsoft.com/en-us/dotnet/api/system.type.isinstanceoftype["IsInstanceOfType" Method]
=== Articles & blog posts
* https://stackoverflow.com/a/5737947[Difference between "System.Type" and "System.RuntimeType"]
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
2023-06-01 09:23:16 +02:00
* `.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[]