If you have an object that has a property of some type that is not known by windsor (as example Regex), if you try to configure it with xml the error “No converter registered to handle the type” will occurs. This happens because the container read configuration as string, and then it has to convert to a type it does not know. The solution is to write few line of code to create a converter

public class RegexConverter : AbstractTypeConverter { public override bool CanHandleType(Type type) { return type == typeof (Regex); } public override object PerformConversion(global::Castle.Core.Configuration.IConfiguration configuration, Type targetType) { return PerformConversion(configuration.Value, targetType); } public override object PerformConversion(string value, Type targetType) { return new Regex(value); } }

A converter has three methods, the first CanHandleType is called to check if this converter can handle various type. In our implementation I return true only if the type is a Regular Expression. The remaining two methods PerformConversion are called to actually perform the desidered conversion. In our situation the conversion is straightforward because you can simply use the constructor of the Regex that accepts a string.

To register this converter into the container you need to use these snippet

globalContainer = new WindsorContainer( new XmlInterpreter(new ConfigResource(cCastleWindsorConfigSection))); IConversionManager manager = (IConversionManager) globalContainer.Kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey); manager.Add(new RegexConverter());

You get the IConversionManager by Kernel.GetSubSystem, and then add your new converter…the game is done.

alk.

Tags:

kick it on DotNetKicks.com

 




kick it on DotNetKicks.com

One Response to “Castle Windsor "No converter registered to handle the type"”

Trackbacks/Pingbacks

  1. Alkampfer’s Place » Blog Archives » Castle windsor converter for Regex

Leave a Reply