Monday, June 22, 2009

__clrtype__ Metaclasses: Positional Attribute Parameters

Harry Pierson (the IronPython Program Manager for Microsoft) has been exploring and explaining the new __clrtype__ feature coming in IronPython 2.6 (or already available if you are using the beta).

When we last left him Harry had been creating Python classes with simple .NET attributes applied to members. In this post he delves further and applies attributes that take parameters:
The basic infrastructure for custom attributes in IronPython is in place, but it’s woefully limited. Specifically, it only works for custom attributes that don’t have parameters. Of course, most of the custom attributes that you’d really want to use require additional parameters, both the positional or named variety. Since positional parameters are easier, let’s start with them.

Positional parameters get passed to the custom attribute’s constructor. As we saw in the previous post, you need a CustomAttributeBuilder to attach a custom attribute to an attribute target (like a class). Previously, I just needed to know the attribute type since I was hard coding the positional parameters. But now, I need to know both the attribute type as well as the desired positional parameters. I could have built a custom Python class to track this information, but it made much more sense just to use CustomAttributeBuilder instances. I built a utility function make_cab to construct the CustomAttributeBuilder instances.

No comments:

Post a Comment

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