No, virtual methods are called through virtual dispatch .
Reflection is not used here. And for no virtual method calls. I believe that the documentation for the exception is a little misleading in the fact that exceptions of this type are generated by reflection-induced methods, but not exclusively.
If someone is wondering why the code in the question gives an exception, this is because of the order in which the constructors are executed. ClassB
constructor is the same as:
public ClassB() : base() { _output = "Constructor has occured"; }
Note the call to base()
, it calls the base constructor before starting the ClassB
constructor and, therefore , before _output is assigned . The virtual SplitTheWords
method SplitTheWords
called in the base constructor, which allows ClassB.SplitTheWords
. This method tries to use _output
, therefore an error.
A more detailed look at why virtual methods should not be called from designers by this SO question contains some useful information. Eric Lippert also has a very good blog post about why this is happening here .
source share