• 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;
            }

        }

  • 相关阅读:
    被忽视的调试工具Swagger
    MongoDB操作
    js获取当月第一天和最后一天
    vue中 关于$emit的用法
    map和flatmap的区别
    element 的el-dialog 浮层嵌套,第二次弹出的会被遮住
    el-table加背景色
    java 正则表达式匹配
    Python自动化测试 (七)logging 日志模块
    git安装配置与使用
  • 原文地址:https://www.cnblogs.com/chenjunbiao/p/1760214.html
Copyright © 2020-2023  润新知