• Castle入门示例记录01


    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Castle.Windsor;
    using Castle.Windsor.Configuration.Interpreters;
    using Castle.MicroKernel.Registration;

    namespace TestCastleWinApp1
    {
        
    class Program
        {
            
    static void Main(string[] args)
            {
                
    //建立容器
                
    //IWindsorContainer container = new WindsorContainer( new XmlInterpreter("http://www.cnblogs.com/BasicUsage.xml") );
                IWindsorContainer container = new WindsorContainer(new XmlInterpreter("CastleConfig.xml"));

                
    //加入组件
                
    //container.AddComponent("txtLog", typeof(ILog), typeof(TextFileLog));
                
    //container.AddComponent("sdfsdfsfsdf", typeof(ILogFormatter), typeof(TextFormatter));

                
    //警告    1    “Castle.Windsor.IWindsorContainer.AddComponent(string, System.Type, System.Type)”已过时:
                
    //“Use Register(Component.For(serviceType).ImplementedBy(classType).Named(key)) instead.”    
                
    //D:\wucg\test\TestProjects\TestCastleWinApp1\TestCastleWinApp1\Program.cs    18    13    TestCastleWinApp1
                
                container.Register(Component.For(
    typeof(ILog)).ImplementedBy(typeof(TextFileLog)).Named("txtLog"));
                container.Register(Component.For(
    typeof(ILogFormatter)).ImplementedBy(typeof(TextFormatter)).Named("sdfsdf"));
                
                
    //获取组件
                
    //instantiate and configure root component and all its dependencies
                
    //ILog log = (ILog)container["txtLog"];     //已过时
                ILog log = (ILog)container.Resolve("txtLog"typeof(TextFileLog));
                
    //使用组件
                log.Write("这是我的输出内容");
                var log2 
    = container.Resolve<ILog>();      //("txtLog", typeof(TextFileLog));
                log2.Write("这是我的输出内容222222222");
                
                Console.WriteLine(
    object.ReferenceEquals(log, log2));   //同一个对象True

                container.Dispose();        
    // clean up
                Console.ReadLine();

            }
        }
    }
    项目文件: /Files/wucg/_TestProjects/TestCastleWinApp1.zip
  • 相关阅读:
    非985、211毕业,却选择了拒接百度Android开发岗offer?
    第五章:Redis持久化-RDB持久化
    第一章:初识Redis
    第四章:虚拟机监控工具
    第三章:内存分配与回收策略
    第三章:垃圾回收器-参数总结
    第一章:Nginx安装
    第一章:Nginx介绍
    第三章:进程管理-进程描述符及任务结构
    4、跃进表
  • 原文地址:https://www.cnblogs.com/wucg/p/1838503.html
Copyright © 2020-2023  润新知