Reflection and IOC

Source: Internet
Author: User

Reflection

The reflection mechanism is in the running state, for any class, can know all the properties and methods of this class, for any one object, can call any of its methods; This dynamically acquired and dynamically called method of the object is called the reflection mechanism. The reflection mechanism dynamically acquires the method and uses the method to directly invoke the object directly when it creates a class. For example, a class has a property or method that is private, and we cannot call it directly, but we can use the reflection mechanism to invoke it dynamically.

IOC

The great thing about IOC is that object generation is defined in XML, so when we need to change the implementation subclass it will become very simple (generally such objects are implemented in some kind of interface), as long as the XML can be modified, This way we can even implement hot-swappable objects (a bit like USB and SCSI drives). Before the IOC does not apply to an object if it relies on another object (hereafter we simply referred to as the dependent object and the dependent object), we want to instantiate a dependent object in the dependent object so that the method in the dependent object can be called. Obviously, this is a high degree of coupling, which does not conform to our programming principles. So at this point we introduce a third-party object that is responsible for conveying a dependent object directly to the dependent object, reducing the coupling between the two. Is the comparison of the coupling degree of the object in the system before and after the IOC container is added


Before the introduction of the IOC container, the software system 1 shows that object a relies on object B, so when object a initializes or runs to a certain point, it must take the initiative to create object B or Use object B that has already been created. Whether you are creating or using object B, you have control over your own hands.
After the introduction of the IOC container, the software system completely changed, 2, as the IOC container joins, object A and object B lose direct contact, so when object a runs to object B, the IOC container will actively create an object B to inject to object a where it is needed.
Through the contrast, we do not look ugly: object A to rely on the process of object B, from the active behavior to passive behavior, control reversed, this is the name of "control reversal" the origin.

Instance

Reflection Instance Code


<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #666666;" ><strong>using system;using system.collections.generic;using system.linq;using System.Text;using         System.threading.tasks;namespace studentdal{public class Student {//property public string Name{get;set;}        public int Age {get; set;} No parameter constructor public Student () {this.            Name = "no parameter"; This.        Age = 0; }//There is a parameter constructor public Student (string name, int.) {this.            Name = "Name"; This.        Age = age; }//public with the return value function public string Publishmethodreturn () {return string.        Format ("I Call" +name+ "age" +age); }}}</strong></span> 
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #666666;" ><strong>using system;using system.collections.generic;using system.linq;using System.Text;using System.threading.tasks;using System.reflection;namespace itoo_reflection{class Program {static void Main (ST            Ring[] (args) {//Use Assembly to define and load assemblies,//load modules listed in the assembly manifest,//And find types from this assembly and create instances of that type.            Get assembly Assembly Assembly = Assembly.Load ("Studentdal"); From the program and get the specified object type, types type = assembly.            GetType ("Studentdal.student"); var instance = assembly.            CreateInstance ("Studentdal.student"); Assigns a value type to the property of the student class. GetProperty ("Name").            SetValue (instance, "Shx", null); Type. GetProperty ("Age").            SetValue (instance, +, NULL); Get the Student class by var method = type.            GetMethod ("Publishmethodreturn"); Call the Member method of the Student class Publishmethodreturn var s=. Invoke (instance, nULL);            Console.WriteLine (S);        Console.read (); }}}</strong></span>


Run results



IOC instance Code



<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #666666;" ><strong>using system;using system.collections.generic;using system.linq;using System.Text;namespace ITOO. IOC. idal{public    Interface Iuserdal    {        void Helloword ();}    } </strong></span>
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #666666;" ><strong>using system;using system.collections.generic;using system.linq;using System.Text;using ITOO. IOC. Idal;namespace Itoo. IOC. dal{public    class User:iuserdal    {public        void Helloword ()        {            Console.WriteLine ("Helloword");        }    }} </strong></span>
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #666666;" ><strong>using system;using system.collections.generic;using system.linq;using System.Text;namespace ITOO. IOC. ibll{public    Interface Iuserbll    {        void Helloword ();}    } </strong></span>
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #666666;" ><strong>using system;using system.collections.generic;using system.linq;using System.Text;using ITOO. IOC. Ibll;using Itoo. IOC. Idal;using Itoo. Library.core.aop;namespace Itoo. IOC. bll{public class Userbll:iuserbll {public void Helloword () {//Springhelper from the IOC container using the underlying package            Get the object instance of Class D Iuserdal iuser = springhelper.getobject<iuserdal> ("User"); Iuser.        Helloword (); }}}</strong></span> 
<span style= "Font-family:kaiti_gb2312;font-size:18px;color: #666666;" ><strong>using system;using system.collections.generic;using system.linq;using System.Text;using ITOO. IOC. Ibll;using Itoo. Library.core.aop;namespace Itoo. IOC. client{    class program    {        static void Main (string[] args)        {            // The client gets an instance of the USERBLL class from the IOC container, based on the ID of the object of the B-tier class, from the springhelper of the underlying package            IUSERBLL IUSERBLL = SPRINGHELPER.GETOBJECT<IUSERBLL > ("USERBLL");            Call the method Iuserbll of the Userbll class            . Helloword ();            Console.read ();}}}    </strong></span>


Run results



The above is the reflection and IOC content, more relevant content please pay attention to topic.alibabacloud.com (www.php.cn)!


  • 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.