Ioc Unity

wKioL1ZNkcixbPksAAASpaF8JY8682.png

<?xml version="1.0" encoding="utf-8" ?>
<configuration>

  <configSections>
    <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection,Microsoft.Practices.Unity.Configuration"/>
  </configSections>

  <unity>
    <containers>
      <container>
        <register type="ConsoleApplication4.IUser,ConsoleApplication4" mapTo="ConsoleApplication4.User,ConsoleApplication4"></register>
      </container>
    </containers>
  </unity>
  
</configuration>


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

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

            unity.Configure(container);

            Console.WriteLine(container.Resolve<IUser>().aa());

            Console.ReadKey();

        }
    }
}


你可能感兴趣的:(IOC,unity)