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
posted @ 2010-09-29 15:35  庚武  Views(391)  Comments(0Edit  收藏  举报