zoukankan      html  css  js  c++  java
  • Castle的自定义类型转换

    自定义类型Observer的定义如下:
        [Serializable]
        
    public class Observer
        
    {
            
    /// <summary>
            
    /// IUpdatingSender的实现
            
    /// </summary>

            //[XmlAttribute(AttributeName="IUpdatingSenderImplement")]
            public string IUpdatingSenderImplement getset; }
        }

    现希望通过Castle注入属性
    public Observer[] Observers getset; }

    因为是数据配置文件可以写入如下形式
                    <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);
            }

        }

    其中(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;
            }

        }

  • 相关阅读:
    dropdownlist绑定和选中
    uploadify ,前端页面获取值,json,ajax
    sql 列设置默认值,语法查询知识点积累
    两个string数组对应比较
    镜像下载-收藏下
    我用shell写了个mud游戏:武林群侠传
    【shell】十分钟轻松入门;如果没入门,您吐口口水再走吧!
    【命令详解001】top
    那些未曾忘怀的
    mysql慢查询分析工具比较与实战
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/1760214.html
Copyright © 2011-2022 走看看