引用的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(); } } }