zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    JavaScript
    css-装饰
    html 标签
    remote connect openshift mysql
    MySQL
    how to run a continuous background task on OpenShift
    openshifit 安装 redis
    Python之路,Day6
    选择排序
    C语言实现链表
  • 原文地址:https://www.cnblogs.com/wucg/p/1838503.html
Copyright © 2011-2022 走看看