IronPython is a great way to use the .NET framework. It comes packed full of Python dynamic goodness. Unfortunately it isn't perfect. One noteworthy hole in the IronPython .NET integration is attributes. You can't use attributes in IronPython, which can sometime be a problem.

IronPython and .NET Attributes With Python you can create new classes at runtime, modify them, and even switch out their __class__ or __bases__ attributes. This makes them very different to C# classes. IronPython class is actually a .NET object rather than a true class. This means that you can't treat IronPython classes as .NET classes, and you can't (yet) use attributes. One way round this would be to use the Reflection.Emit API (on which IronPython is built). There is a good codeproject article giving an introoduction to the subject, but that piece of magic will have to be the subject of another IronPython article. This article is about an alternative approach - dynamically generating and compiling C#.

The normal way round this problem is to create stub C# classes with methods that you can override in IronPython. This doesn't always work though; sometimes you want to dynamically specify the arguments to the attributes - which can only be done at compile time with C#.

This article explores a way round the problem, with a solution that potentially has many other uses. It provides a way to dynamically compile C# source code into assemblies. These assemblies can be used in memory or saved to disk.