Code Inspection: Access to a static member of a type via a derived type
This error arises in code that accesses a static member of a type via a type that was derived from it. For example:
The above code leads to the erroneous perception that the field
N belongs to the type
B, whereas it does not. Furthermore, if types
B are introduced in different assemblies, then the
Main() method would introduce an unneeded dependency to
B's assembly, which could otherwise be avoided.
An even more confusing situation can arise from the call to a factory method. If you define a variable with
var, the type you get may not be what you would expect. For example
The above code suggests that the result of the
Create() operation is an object of type
HttpWebRequest whereas it's not actually the case - the result is of type
WebRequest and is best written as follows:
Also, C# compiler creates call to method using declaring type, so in IL it will in fact be call to base type. If you add method with the
new modifier and same signature to derived type later, code behaviour will not change unless recompiled.