• Unity之再入江湖


    引用的dll:

    Microsoft.Practices.Unity.Configuration.dll
    Microsoft.Practices.Unity.dll

    app.config

    <?xml version="1.0"?>
    <configuration>
      <configSections>
        <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration"/>
      </configSections>
      <startup>
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.0,Profile=Client"/>
      </startup>
    
      <unity>
        <containers>
          <!--容器-->
          <container>
            <!--映射关系-->
            <register type="LifeManagerInUnity.IA, LifeManagerInUnity" mapTo="LifeManagerInUnity.A, LifeManagerInUnity"/>
            <register type="LifeManagerInUnity.IB, LifeManagerInUnity" mapTo="LifeManagerInUnity.B, LifeManagerInUnity"/>
            <register type="LifeManagerInUnity.IC, LifeManagerInUnity" mapTo="LifeManagerInUnity.C, LifeManagerInUnity"/>
            <register type="LifeManagerInUnity.ID, LifeManagerInUnity" mapTo="LifeManagerInUnity.D, LifeManagerInUnity"/>
          </container>
        </containers>
      </unity>
    </configuration>
    

     类

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace LifeManagerInUnity
    {
        public interface IA 
        { 
            string Description { get; set; }
            void ShowInfo();
        }
        public interface IB 
        { 
            string Description { get; set; }
            void ShowInfo();
        }
    
        public interface IC 
            { 
            string Description { get; set; }
            void ShowInfo();
        }
        public interface ID
        {
            string Description { get; set; }
            void ShowInfo();
        }
    
    
        public class A : IA 
        { 
            public string Description { get; set; }
    
            public void ShowInfo()
            {
                Console.WriteLine(Description);
            }
        }
        public class B : IB 
        { 
            public string Description { get; set; }
            public void ShowInfo()
            {
                Console.WriteLine(Description);
            }
        }
        public class C : IC 
        { 
            public string Description { get; set; }
            public void ShowInfo()
            {
                Console.WriteLine(Description);
            }
        }
        public class D : ID 
        { 
            public string Description { get; set; }
            public void ShowInfo()
            {
                Console.WriteLine(Description);
            }
        }
    }                       
    

     使用实例

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Practices.Unity;
    using Microsoft.Practices.Unity.Configuration;
    using System.Configuration;
    
    
    namespace LifeManagerInUnity
    {
        class Program
        {
            static void Main(string[] args)
            {
                IA a1 = new A() { Description = Guid.NewGuid().ToString() };
                IA a2 = new A() { Description = Guid.NewGuid().ToString() };
    
                IB b1 = new B() { Description = Guid.NewGuid().ToString() };
    
                IUnityContainer container = new UnityContainer();
                UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection(UnityConfigurationSection.SectionName);
                configuration.Configure(container);
    
                Console.WriteLine(container.Resolve<IA>().GetHashCode());
                Console.WriteLine(container.Resolve<IA>().GetHashCode());
    
                container.RegisterInstance<IA>("A2", a2);
                container.RegisterInstance<IA>(a1);
               
                Console.WriteLine(container.Resolve<IA>().GetHashCode());
                Console.WriteLine(container.Resolve<IA>().GetHashCode());
            
                Console.WriteLine(container.Resolve<IA>("A2").GetHashCode());
                Console.WriteLine(container.Resolve<IA>("A2").GetHashCode());
    
                Console.WriteLine(container.Resolve<IA>().GetHashCode());
    
    
                Console.Read();
            }
        }
    }
    
  • 相关阅读:
    C#基础笔记(第二十一天)
    C#基础笔记(第十九天)
    C#基础笔记(第十八天)
    C#基础笔记(第十七天)
    C#基础笔记(第十六天)
    C#基础笔记(第十五天)
    [PyTorch 学习笔记] 6.1 weight decay 和 dropout
    PyTorch ResNet 使用与源码解析
    [PyTorch 学习笔记] 5.2 Hook 函数与 CAM 算法
    [PyTorch 学习笔记] 5.1 TensorBoard 介绍
  • 原文地址:https://www.cnblogs.com/easy5weikai/p/2961908.html
Copyright © 2020-2023  润新知