Castle的自定义类型转换
自定义类型Observer的定义如下:
现希望通过Castle注入属性
因为是数据配置文件可以写入如下形式
因为Observer不是系统自带类型,需要为其编写自定义转换的实现
其中(Observer)Serializer.UTF8.ConvertToObject(value, targetType);实质是把
<![CDATA[<Observer><IUpdatingSenderImplement>UpdatingSenderForUpdatingObserver1</IUpdatingSenderImplement></Observer>]]>
的内容反序化为Observer对象
完成ObserverTypeConverter的定义后需要添加到容器IConversionManager中.
[Serializable]
public class Observer
{
/// <summary>
/// IUpdatingSender的实现
/// </summary>
//[XmlAttribute(AttributeName="IUpdatingSenderImplement")]
public string IUpdatingSenderImplement { get; set; }
}
public class Observer
{
/// <summary>
/// IUpdatingSender的实现
/// </summary>
//[XmlAttribute(AttributeName="IUpdatingSenderImplement")]
public string IUpdatingSenderImplement { get; set; }
}
现希望通过Castle注入属性
public Observer[] Observers { get; set; }
因为是数据配置文件可以写入如下形式
<Observers>
<item type="CSDN.Space.UpdatingService.ComponentModel.Observer">
<item><![CDATA[<Observer><IUpdatingSenderImplement>UpdatingSenderForUpdatingObserver1</IUpdatingSenderImplement></Observer>]]></item>
<item><![CDATA[<Observer><IUpdatingSenderImplement>UpdatingSenderForUpdatingObserver2</IUpdatingSenderImplement></Observer>]]></item>
</item>
</Observers>
<item type="CSDN.Space.UpdatingService.ComponentModel.Observer">
<item><![CDATA[<Observer><IUpdatingSenderImplement>UpdatingSenderForUpdatingObserver1</IUpdatingSenderImplement></Observer>]]></item>
<item><![CDATA[<Observer><IUpdatingSenderImplement>UpdatingSenderForUpdatingObserver2</IUpdatingSenderImplement></Observer>]]></item>
</item>
</Observers>
因为Observer不是系统自带类型,需要为其编写自定义转换的实现
public class ObserverTypeConverter : AbstractTypeConverter
{
private NLog.Logger logger = NLog.LogManager.GetLogger("ObserverTypeConverter");
public override bool CanHandleType(Type type)
{
return type.IsAssignableFrom(typeof(Observer));
}
public override object PerformConversion(string value, Type targetType)
{
return (Observer)Serializer.UTF8.ConvertToObject(value, targetType);
}
public override object PerformConversion(IConfiguration configuration, Type targetType)
{
//logger.Debug("Name={0},Value={1},Count={2}", configuration.Name, configuration.Value, configuration.Attributes.Count);
return PerformConversion(configuration.Value, targetType);
}
}
{
private NLog.Logger logger = NLog.LogManager.GetLogger("ObserverTypeConverter");
public override bool CanHandleType(Type type)
{
return type.IsAssignableFrom(typeof(Observer));
}
public override object PerformConversion(string value, Type targetType)
{
return (Observer)Serializer.UTF8.ConvertToObject(value, targetType);
}
public override object PerformConversion(IConfiguration configuration, Type targetType)
{
//logger.Debug("Name={0},Value={1},Count={2}", configuration.Name, configuration.Value, configuration.Attributes.Count);
return PerformConversion(configuration.Value, targetType);
}
}
其中(Observer)Serializer.UTF8.ConvertToObject(value, targetType);实质是把
<![CDATA[<Observer><IUpdatingSenderImplement>UpdatingSenderForUpdatingObserver1</IUpdatingSenderImplement></Observer>]]>
的内容反序化为Observer对象
完成ObserverTypeConverter的定义后需要添加到容器IConversionManager中.
internal class ContainerBuilder
{
private static readonly object ton = new object();
private static IWindsorContainer container = null;
public static IWindsorContainer Create()
{
string subPath = ConfigurationManager.AppSettings["UpdatingService.Components.ConfigurationPath"];
string fileName = "";
HttpContext context = HttpContext.Current;
if (context != null)
{
fileName = context.Server.MapPath(String.Format("~/{0}", subPath));
}
else
{
//支持非网站程序
fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, subPath);
}
IWindsorContainer iWindsorContainer = Create(fileName);
//添加类型转换器
IKernel kernel = iWindsorContainer.Kernel;
IConversionManager conversionManager = (IConversionManager)kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey);
conversionManager.Add(new ObserverTypeConverter());
return iWindsorContainer;
}
private static IWindsorContainer Create(string fileName)
{
if (container == null)
{
lock (ton)
{
if (container == null)
{
container = new WindsorContainer(new XmlInterpreter(fileName));
}
}
}
return container;
}
}
{
private static readonly object ton = new object();
private static IWindsorContainer container = null;
public static IWindsorContainer Create()
{
string subPath = ConfigurationManager.AppSettings["UpdatingService.Components.ConfigurationPath"];
string fileName = "";
HttpContext context = HttpContext.Current;
if (context != null)
{
fileName = context.Server.MapPath(String.Format("~/{0}", subPath));
}
else
{
//支持非网站程序
fileName = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, subPath);
}
IWindsorContainer iWindsorContainer = Create(fileName);
//添加类型转换器
IKernel kernel = iWindsorContainer.Kernel;
IConversionManager conversionManager = (IConversionManager)kernel.GetSubSystem(SubSystemConstants.ConversionManagerKey);
conversionManager.Add(new ObserverTypeConverter());
return iWindsorContainer;
}
private static IWindsorContainer Create(string fileName)
{
if (container == null)
{
lock (ton)
{
if (container == null)
{
container = new WindsorContainer(new XmlInterpreter(fileName));
}
}
}
return container;
}
}