Unity in C #

Source: Internet
Author: User

Tag:sys   arp    Introduction    array   logger    first   coding   soft   sch   

1. Referencing objects 2. Configuring in app. Config <?xml version= "1.0" encoding= "Utf-8"?><configuration> <configSections> &                                Lt;section name= "Unity" type= "Microsoft.Practices.Unity.Configuration.UnityConfigurationSection, Microsoft.Practices.Unity.Configuration "/> </configSections> <unity configsource=" Unity. Config "/> </configuration> here Unity's configuration is configured in Unity.config. The code is as follows: <unity xmlns= "http://schemas.microsoft.com/practices/2010/unity" > <!--alias definition name--<alias alias= "IClass" type= "Consoleapplication1.iclass, ConsoleApplication1"/> <alias alias= "MyClass" type= " Consoleapplication1.myclass, ConsoleApplication1 "/> <!--introduced the following namespace, you can save too much input--<namespace name=" ConsoleApplication1 "/> <assembly name=" ConsoleApplication1 "/> <container > <register type=" Icla SS "mapto=" MyClass "/> <register type=" ILogger "mapto=" FileLogger "></register> <! --Registration class + constructor default value is 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=" + "/> &lt ;/method> </register> <register type= "ILogger" mapto= "Mocklogger" name= "Validlogger"/> <regist Er type= "objectusinglogger" name= "dependencyregistered" > <property name= "Logger" > <optional name= " Validlogger "/> </property> </register> <!--values---&Lt;register type= "ILogger" name= "main" mapto= "Mocklogger"/> <register type= "ILogger" Name= "another" mapto= "Mock Logger "/> <register type=" ILogger "name=" Special "mapto=" Speciallogger "/> <register type=" ArrayDepende Ncyobject "name=" specificelements "> <property name=" loggers "> <array> <dependency N Ame= "main"/> <dependency name= "another"/> </array> </property> </registe R> </container> </unity>3. About configuration <!--alias definition name--<alias alias= "IClass" type= "Consoleapplicati On1. IClass, ConsoleApplication1 "/> <!--introduced the following namespace, you can save too much input--<namespace name=" ConsoleApplication1 "/>            4. Example 4.1 defines UnityContainer and initializes the data in the configuration file var container = new UnityContainer ();                     Unityconfigurationsection configuration = (unityconfigurationsection) configurationmanager.getsection ("Unity"); Configuration. Configure (container); 4.Register simple interface <register type= "IClass" mapto= "MyClass"/> Code as follows: interface IClass {void Showinfo ();        }class Myclass:iclass {[dependency]//Instance Session object public virtual Ipubcontext Context {set; get;}        #region IClass member public void Showinfo () {Console.WriteLine ("MYCLASS12"); } #endregion} call: IClass ClassInfo = container. Resolve<iclass> (); classinfo.showinfo (); 4.3 If there are attributes in the object that need to be initialized and can be instantiated on the property, the property is initialized when the object is obtained.    As in the previous example, the Ipubcontext property.    Class Ipubcontext {public string Name {set; get;} }4.4 instance configuration with constructors <register type= "Installerbase" mapto= "Bloginstaller" > <constructor> <param N Ame= "Pubcontext" type= "Ipubcontext"/> <param name= "sum" type= "System.Int32" value= "3"/> </constr Uctor> </register>
The definition type is as follows: Class Bloginstaller:installerbase {private Ipubcontext pubcontext; private int sum; Public Bloginstaller (Ipubcontext pubcontext,int sum) {this.pubcontext = Pubcontext; } #region installerbase member public void Showinfo () {Console.WriteLine ("Filelogger12install Erbase "); } #endregion} interface installerbase {void Showinfo (); }
The code is called as follows: installerbase classinstallerbase = container. Resolve<installerbase> (); Classinstallerbase.showinfo (); Where the constructor is: public Bloginstaller (Ipubcontext pubcontext,int sum) {th Is.pubcontext = Pubcontext; The parameter in the configuration file corresponds to one by one. And in the configuration file, with the sum attribute, there is a default value of 3.4.5 generic type configuration <register type= "isomeinterface[]" mapto= "mytypeimplementingsomeinterface[]" The/> code is defined as follows: public interface Isomeinterface<t> {} public class Mytypeimplementingsomeinterface<t&gt ; : isomeinterface<t> {} code is called as follows:isomeinterface<int> some = container. Resolve<isomeinterface<int>> (); 4.6 Execution of the configuration method <register type= "Objectwithoverloads" Name= " Callfirstoverload "> <method name=" CallMe "> <param name=" param "type=" int "value=" + "/> &lt ;/method> </register>
Define class Callfirstoverload, and define methods CallMe://<summary>/////</summary> class Objectwithoverloads {public int firstoverloadcalls; public int secondoverloadcalls; public void CallMe (int param) {++firstoverloadcalls; } public void CallMe (string param) {++secondoverloadcalls; }} Code call://Execute method var result = container. Resolve<objectwithoverloads> ("Callfirstoverload"); Console.WriteLine (Result. Firstoverloadcalls); 4.7 Array Configuration <!--number of <register type= "ILogger" name= "main" mapto= "Mocklogger"/> < Register type= "ILogger" Name= "another" mapto= "Mocklogger"/> <register type= "ILogger" name= "Special" mapto= "Spec Iallogger "/> <register type=" Arraydependencyobject "name=" specificelements "> <property name=" Loggers " > <array> <dependency name= "main"/> <dependency name= "AnothEr "/> </array> </property> </register> Copy Code
Definition classes: Class Arraydependencyobject {public ilogger[] loggers {get; set;} Public string[] Strings {get; set;} } class Speciallogger:ilogger {#region ILogger member public void Showinfo () {throw New NotImplementedException (); } #endregion} code call: var result2 = container. Resolve<arraydependencyobject> ("Specificelements"), you can see that the result2 execution is complete and you get result2. Loggers group, this array consists of 2 objects, one is main and the other is another, which are ILogger objects.

  

Unity in C #

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.