Thursday 24 September 2009

Configuration of generic types in Spring.Net

Whe using Spring.Net's XML configuration files defining generic types requires a non-intuitive trick or two.

1. The left bracket () must be written using XML escape syntax (i.e. <).
2. Generic type arguments can not be fully assembly qualified as the comma is used to separate generic type arguments.

A generic object definition might look like this:

<object id="myFilteredIntList" type="GenericsPlay.FilterableList&lt;int>, GenericsPlay">
    <property name="Name" value="My Integer List"/>
</object>

Note the use of &lt;.

In the case of item 2 above it is suggested you use type aliases to clarify the text:

<typeAliases>
    <alias name="GenericDictionary" type=" System.Collections.Generic.Dictionary&lt;,>" />
</typeAliases>
<object id="myGenericObject"
type="GenericsPlay.ExampleGenericObject&lt;GenericDictionary&lt;int , string>>, GenericsPlay" />

instead of:

<object id="myGenericObject"
type="GenericsPlay.ExampleGenericObject&lt;System.Collections.Generic.Dictionary&lt;int , string>>, GenericsPlay" />

Yuk.

See section 5.2.6.1 of the Spring.Net documentation for details.