标签:sys arp 引入 array logger first coding soft sch
1.引用对象 2.在app.config中进行配置 <?xml version="1.0" encoding="utf-8" ?> <configuration> <configSections> <section name="unity" type="Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration" /> </configSections> <unity configSource="unity.config" /> </configuration> 这里unity的配置是在unity.config中进行配置的。 代码如下: <unity xmlns="http://schemas.microsoft.com/practices/2010/unity"> <!--alias 定义別名--> <alias alias="IClass" type="ConsoleApplication1.IClass, ConsoleApplication1" /> <alias alias="MyClass" type="ConsoleApplication1.MyClass, ConsoleApplication1" /> <!--引入下面的命名空间,可以省的输入过多的內容--> <namespace name="ConsoleApplication1" /> <assembly name="ConsoleApplication1" /> <container > <register type="IClass" mapTo="MyClass" /> <register type="ILogger" mapTo="FileLogger"></register> <!--注册类+构造函数 默认值是3--> <register type="InstallerBase" mapTo="BlogInstaller"> <constructor> <param name="pubContext" type="IPubContext" /> <param name="sum" type="System.Int32" value="3" /> </constructor> </register> <!--<register type="ISomeInterface[]" mapTo="MyTypeImplementingSomeInterface[]"/>--> <register type="ISomeInterface[System.Int32]" mapTo="MyTypeImplementingSomeInterface[System.Int32]"/> <register type="ObjectWithOverloads" name="callFirstOverload"> <method name="CallMe"> <param name="param" type="int" value="17"/> </method> </register> <register type="ILogger" mapTo="MockLogger" name="validLogger" /> <register type="ObjectUsingLogger" name="dependencyRegistered"> <property name="Logger"> <optional name="validLogger" /> </property> </register> <!--数值--> <register type="ILogger" name="main" mapTo="MockLogger" /> <register type="ILogger" name="another" mapTo="MockLogger" /> <register type="ILogger" name="special" mapTo="SpecialLogger" /> <register type="ArrayDependencyObject" name="specificElements"> <property name="Loggers"> <array> <dependency name="main" /> <dependency name="another" /> </array> </property> </register> </container> </unity> 3.关于配置 <!--alias 定义別名--> <alias alias="IClass" type="ConsoleApplication1.IClass, ConsoleApplication1" /> <!--引入下面的命名空间,可以省的输入过多的內容--> <namespace name="ConsoleApplication1" /> 4.实例说明 4.1 定义UnityContainer,并初始化用配置文件中的数据 var container = new UnityContainer(); UnityConfigurationSection configuration = (UnityConfigurationSection)ConfigurationManager.GetSection("unity"); configuration.Configure(container); 4.注册简单接口 <register type="IClass" mapTo="MyClass" /> 代码如下: interface IClass { void ShowInfo(); } class MyClass:IClass { [Dependency]//实例话对象 public virtual IPubContext Context { set; get; } #region IClass 成员 public void ShowInfo() { Console.WriteLine("MyClass12"); } #endregion } 调用: IClass classInfo = container.Resolve<IClass>(); classInfo.ShowInfo(); 4.3 如果对象中有属性,需要初始化,可以在属性上加[Dependency]//实例化对象,那么在取得对象时,这个属性会被初始化。 如上例中的IPubContext 属性。 class IPubContext { public string Name { set; get; } } 4.4 带构造函数的实例配置 <register type="InstallerBase" mapTo="BlogInstaller"> <constructor> <param name="pubContext" type="IPubContext" /> <param name="sum" type="System.Int32" value="3" /> </constructor> </register>
定义类型如下: class BlogInstaller : InstallerBase { private IPubContext pubContext; private int sum; public BlogInstaller(IPubContext pubContext,int sum) { this.pubContext = pubContext; } #region InstallerBase 成员 public void ShowInfo() { Console.WriteLine("FileLogger12InstallerBase"); } #endregion } interface InstallerBase { void ShowInfo(); }
代码调用如下: InstallerBase classInstallerBase = container.Resolve<InstallerBase>(); classInstallerBase.ShowInfo(); 其中,构造函数是: public BlogInstaller(IPubContext pubContext,int sum) { this.pubContext = pubContext; } 与配置文件中的参数是一一对应的。并且配置文件中对与sum这个属性,有默认数值是3. 4.5 泛型类型配置 <register type="ISomeInterface[]" mapTo="MyTypeImplementingSomeInterface[]"/> 代码如下定义: public interface ISomeInterface<T> { } public class MyTypeImplementingSomeInterface<T> : ISomeInterface<T> { } 代码调用如下: ISomeInterface<int> some = container.Resolve<ISomeInterface<int>>(); 4.6 配置方法的执行 <register type="ObjectWithOverloads" name="callFirstOverload"> <method name="CallMe"> <param name="param" type="int" value="17"/> </method> </register>
定义类callFirstOverload,并且定义方法CallMe: /// <summary> /// 执行方法 /// </summary> class ObjectWithOverloads { public int FirstOverloadCalls; public int SecondOverloadCalls; public void CallMe(int param) { ++FirstOverloadCalls; } public void CallMe(string param) { ++SecondOverloadCalls; } } 代码调用: //执行方法 var result = container.Resolve<ObjectWithOverloads>("callFirstOverload"); Console.WriteLine(result.FirstOverloadCalls); 4.7 数组配置 <!--数組--> <register type="ILogger" name="main" mapTo="MockLogger" /> <register type="ILogger" name="another" mapTo="MockLogger" /> <register type="ILogger" name="special" mapTo="SpecialLogger" /> <register type="ArrayDependencyObject" name="specificElements"> <property name="Loggers"> <array> <dependency name="main" /> <dependency name="another" /> </array> </property> </register> 复制代码
定义类: class ArrayDependencyObject { public ILogger[] Loggers { get; set; } public string[] Strings { get; set; } } class SpecialLogger : ILogger { #region ILogger 成员 public void ShowInfo() { throw new NotImplementedException(); } #endregion } 代码调用: var result2 = container.Resolve<ArrayDependencyObject>("specificElements"); 可以看到result2执行完成后,会得到 result2.Loggers数組,这个数组包括2个对象,一个是main 另一个是another,都是ILogger对象。
标签:sys arp 引入 array logger first coding soft sch
原文地址:http://www.cnblogs.com/xlxr45/p/7792628.html