Virtual members and overrides
From my own experience, generating new types from scratch that didn’t exist at compile time is not the most common use case. I’ve found myself, more often than not, just wanting to automate something that I find tedious and forced upon me from libraries that I have to use.
When that is the case, it is common to take a type and create a new one that inherits from this and then starts overriding behavior.
Since C# doesn’t have all its members as virtual, as the case is with Java, members have to be explicitly virtual. An example of a method that is virtual is one that all objects inherit – the ToString method.
Let’s continue the work on the MyTypeGenerator code by adding an override of the ToString method, just to see the mechanics of how it is done:
- In the Generate method of the MyTypeGenerator class, before you return the type, you need to define a new method that will be the MyType implementation of...