development

So much work

July 24, 2004 1:10:05.813

This is interesting - a .NET (C#) developer looking at what it would take to add an attribute to an object at runtime:

I wanted to be able to add properties on the fly.  I've changed the idea a bit to look them up on the fly versus adding them.  Using psuedo-code:

// expandable object
MyObject o = new MyObject();

// add the property
o.AddProperty("FirstName", "Justin");

I'm not using these properties in code anywhere.  I'm actually feeding the object to a template system (the C# port of StringTemplate in this case).  Now StringTemplate supports reading properties in the templates like so...

$p.FirstName$

But unfortunately the MyObject type doesn't have a FirstName property.  And StringTemplate uses the Type to do reflection.  Anyway, during my research of IExpando (much like IDispatchEx), I noticed the interface IReflect.  Looking at the docs for it, it contains all the most common methods that people use on Type when performing reflection (GetProperty, GetMethod, etc.).  So if I change StringTemplate to use IReflect vs. the Type class, then I could implement IReflect myself.  Of course, there are problems with this; mainly the fact that PropertyInfo is an abstract class (MemberInfo, the parent class, is also abstract).  So I would have to write implementations of those.

Hm...this seems like more effort than its worth.  Maybe I should just do something like this...

$p.Attributes.FirstName

Attributes would be an IDictionary.  Then I can extend StringTemplate to look at the object.  If it is IDictionary, then use the property name as the key.  The former definitely has more of a "coolness" factor, but the latter would take like 15 minutes.

Heh. As opposed to the following in Smalltalk:

myObject class addInstVarName: 'firstName'. This is one of those telling differences between Smalltalk and languages like Java and C# - it's not that you can't accomplish the same things - it's that you don't bother because of all the work involved. Smalltalk makes the simple things simple, and the hard things possible. Languages like C# and Java make the simple things possible, and the hard things painful...

 Share Tweet This
-->