Tuesday, June 16, 2009

__clrtype__ Metaclasses: Simple Custom Attributes

One of the exciting new features in IronPython 2.6 is the addition of the __clrtype__ metaclass. This allows you to specify and customize the .NET type that backs your Python classes.

This is important because by default new Python classes aren't also .NET classes. This is for various reasons: Python classes are mutable, .NET classes can't be garbage collected and so on. This causes one of the biggest gaps in the IronPython integration; using .NET attributes requires them to be applied to a real .NET class. There are other features, like some kinds of data binding, also a real .NET class.

There are techniques to overcome this, but they're clunky at best. In IronPython in Action I demostrate runtime generation, compilation and subclassing of C# classes as one technique.

At last the __clrtype__ metaclass provides a better way built into IronPython, but it is a pretty low-level feature requiring a lot of work to build on top of it. Harry Pierson, the IronPython program manager, has been showing how to use __clrtype__ to fill some of these use cases. In his first series of articles Harry demonstrated databinding with Silverlight. Now he has started to tackle custom attributes:
Over the course of the next three blog posts, I’m going to build out a mechanism for specifying custom attributes on the CLR type we’re generating via __clrtype__. All the various Builder classes in System.Reflection.Emit support a SetCustomAttribute method that works basically the same way. There are two overloads – the one I’m going to use takes a single CustomAttributeBuilder as a parameter.

For this first post, I’m going to focus on the basic custom attribute infrastructure, so we’re going to use the extremely simple ObsoleteAttribute. While you can pass some arguments to the constructor, for this first post I’m going to use the parameterless constructor. To keep things less confusing, I’m going back to the original version of the Product class, before I introduced CLR fields and properties. The one change I’m making is that I’m adding a list of attributes I want to add to the class.

No comments:

Post a comment

Note: only a member of this blog may post a comment.