TypeConfigurationElement checked-in

Topics: Development Team Discussion, User Discussion
Aug 22, 2007 at 1:12 AM
I just created the Common project to contain Configuration-related extensions.

The new TypeConfigurationElement class is used to support polymorphic configuration elements the same way PolymorphicConfigurationElementCollection works but for a single element. It is similar to the behavior of XmlSerializer and xsi:type attribute.


public class Vehicle : ConfigurationElement { int MaxSpeed { ... }}public class Car : Vehicle { int Wheels { ... } }

public class MySettings : SerializableConfigurationSection
public TypeConfigurationElement<Vehicle> Transport
{ get { return (TypeConfigurationElement<Vehicle>)this"Transport"; }

xml Fragment:

<Transport type="Namespace.Car, Assembly" MaxSpeed="100", Wheels="4"/>

In code:

MySettings settings = ...
Car car = settings.Transport.Element as Car;

It is important to note that the implementation relies on private reflection since most of the required methods on ConfigurationElement base class weren't available.

Next step would be to implement TypeConfigurationElementCollection the same way, allowing collection of polymorphic items without having to name them. Altough key-related features wouldn't be supported by default.

Feedback would be greatly appreciated.