Wednesday, November 18, 2009

Strange missing feature in CSharpCodeProvider

Currently I'm developing a database application for my university study that uses NHibernate. To enable lazy loading of results it is required for all members of mapped class to be virtual, so NHibernate can create proxy that will be replaced by actual object when required. And if class has events they also must be virtual because actually this is the same fields and methods.

On the other hand I've decided to generate those classes from XML definitions. On of the reasons for this is that this classes seems to be just and a set of properties, but they need to implement INotifiyPropertyChanged. I've decided to write a code generator using System.CodeDom members and to gain some experience at working with members of this namespace. However I've found that CSharpCodeProvider doesn't generate virtual events! Despite the fact that I set the appropriate attribute the event is still normal so NHibernate couldn't generate proxy. I've search through CSharpCodeProvider with Reflector and found that in the method GenerateEvent that generates code of event there is no call of a method OutputVTableModifier that inserts "virtual" keyword. VBCodeGenerator also doesn't generate virtual events.

So I wonder whether this is an intentional decision or the bug in the current .NET framework version (I have 3.5 SP 1). Actually I wasn't determined enough to search for this in the older version of .NET.