Based on this documentation it seems that instance methods defined in the System.Object type are not guaranteed to be thread safe.
This basically means that, given any type instance, it is not safe to call the GetType() method concurrently from different threads.
Is this true even for a type that I do know it is actually thread safe (based on documentation or known type implementation) ?
For instance consider the ImmutableList<T> class. This type is documented to be thread safe, so I would expect I can use any instance of this type concurrently from multiple threads in a safe way, even when calling the GetType() method.
Another example is a stateless service class used inside an ASP.NET core application: a steteless class is thread safe by design (because there is no state to mess up when working concurrently on a class instance). So, again in this case, I expect no issues in using the same instance concurrently from multiple threads, even when calling the GetType() method.
In both these cases can I assume that it is actually safe to call the GetType() method concurrently from different threads ?
How did I get to asking myself this question ?
While working with an ASP.NET core application I registered a few stateless services as singleton dependencies and I happened to call GetType() on one of them.
Web applications are multi thread by nature, so I asked myself about the safety of doing this from multiple threads concurrently.
Why this seems confusing to me ?
The main point is that Object.GetType() is not a virtual method, so each type gets the implementation directly from the System.Object class. That's why I checked the documentation of System.Object class itself, even though I was working with a class thread safe by design (being stateless).
What I intuitively think about this
The type information for an object can't change after the object has been created, so intuitively there is no mutable state related with it, so I do not expect any issue in calling GetType() on the same object instance from different threads at the same time.
This kind of assumption is purely based on intuition and, sometimes, stuff are counter-intuitive. Apart from that, the Microsoft docs explicitly states that thread safety is not guaranteed for any instance method of System.Object, so the topic of this question makes sense to me.