unity配置最簡單程序

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.Unity;
using System.Configuration;
using Microsoft.Practices.Unity.Configuration;

namespace ConsoleApplication2
{
    class Program
    {
        static void Main(string[] args)
        {
            var container = new UnityContainer();
            UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection("unity");
            configuration.Configure(container);

            IClass classInfo = container.Resolve<IClass>();
            classInfo.ShowInfo();

            Console.Read();
        }
    }
    public interface IClass
    {
        void ShowInfo();
    }

    public class MyClass : IClass
    {

        public void ShowInfo()
        {
            Console.WriteLine("dddddddddddd");
        }
    }
}

----------------------------------------配置文件-----------------------------
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" />
  </configSections>
  <unity xmlns="http://schemas.microsoft.com/practices/2010/unity">
    <!--alias 定義別名-->
    <alias alias="IClass" type="ConsoleApplication2.IClass, ConsoleApplication2" />
    <alias alias="MyClass" type="ConsoleApplication2.MyClass, ConsoleApplication2" />
    <!--引入下面的命名空間,可以省的輸入過多的內容-->
    <namespace name="ConsoleApplication2" />
    <assembly name="ConsoleApplication2" />

    <container >
      <register type="IClass" mapTo="MyClass" />
    </container>

  </unity>
</configuration>


 

posted @ 2013-12-30 17:44  pnljs  阅读(2010)  评论(0编辑  收藏  举报