Omer van Kloeten's .NET Zen

Programming is life, the rest is mere details

News

Omer van Kloeten's Facebook profile

Omer has been professionally developing applications over the past 8 years, both at the IDF’s IT corps and later at the Sela Technology Center, but has had the programming bug ever since he can remember himself.
As a senior developer at NuConomy, a leading web analytics and advertising startup, he leads a wide range of technologies for its flagship products.

Get Firefox


powered by Dapper 

.NET Resources

Articles :: CodeDom

Articles :: nGineer

Culture

Projects

Part III: Examples for the Using of the Code Document Object Model

Design Time Support - Serializers

When client side components are developed, especially those in direct association with the user interface, they require support of design-time capabilities. These capabilities often include the automatic insertion of code portions into a source file. For instance, should you drag a Button unto a Form and double click it in the Visual Studio.NET designer, an event registration would be added to the form's InitializeComponent method and a new empty method would be created.
Another example is dragging a DataAdapter unto a form, which then lets you define tables and columns in a visually appealing way and then translates your actions into lines of code inside the form.
These lines of code are usually persisted from an object model graph that was manipulated by the component's developer. The developer wrote a method which received the form's code document's object graph before the component's designer came into play, manipulated it according to the component's needs and returned it for any other components to manipulate it as well.

The Recurring Case of the Typed Collection

Dynamic collections are common when working with .NET. The collections supplied with the Framework are usually not for certain types, but for the general type of object, such as ArrayList.
The problems begin when we have to create Typed Collections – dynamic collections that would accept only variables of a certain type. To create such a collection, a new class must be written, which would implement several methods, such as Add, Remove, etc. Think of creating such collections many times over, one for every type you need. Depressing, isn't it?
Today, the C# language does not support generics. This means that we have to code everything manually, creating methods such as:

public void Add (T value)
{
	base.InnerList.Add(value);
}
This method would limit the collection to only receiving variables of type T.

One solution that we can create for this problem using Code DOM is creating the entire implementation, leaving the type dynamic. We could create a method that from this call:

CollectionGenerator.Generate(typeof(int));
Such methods as the following would be generated:
public void Add (int value)
{
	base.InnerList.Add(value);
}

Comments

No Comments

Leave a Comment

(required) 

(required) 

(optional)

(required)