Home  >  Article  >  Backend Development  >  Reflection and IOC

Reflection and IOC

黄舟
黄舟Original
2017-02-23 09:46:482058browse

Reflection

The reflection mechanism is in the running state. For any class, it can know all the attributes and properties of the class. Method; for any object, any of its methods can be called; this function of dynamically obtaining and dynamically calling the object's methods is called the reflection mechanism. The reflection mechanism dynamically obtains methods and uses them, which is completely different from directly creating an object of a class and calling it directly. For example, if there is a property or method in a class that is private, we cannot call it directly, but we can use the reflection mechanism to call it dynamically.

IOC

## The biggest benefit of IOC is that object generation is It is defined in XML, so when we need to change an implementation subclass, it will become very simple (generally such objects are implemented in some kind of interface), just modify the XML, so that we can even realize the hotness of the object. Plug and unplug (a bit like USB interface and SCSI hard drive). Before IOC is not applicable, if an object depends on another object (hereinafter we will refer to it as the dependent object and the dependent object), we must instantiate a dependent object in the dependent object so that the methods in the dependent object can be called. Obviously, this degree of coupling is relatively high and does not conform to our programming principles. Therefore, at this time we will introduce a third-party object, which is responsible for directly delivering a dependent object to the dependent object, reducing the coupling between the two. The following figure shows the comparison of object coupling in the system before and after adding the IOC container


## Before the IOC container was introduced in the software system, as shown in Figure 1, object A depends on object B. Then object A must actively create it when it is initialized or runs to a certain point. Object B or use an already created object B. Whether you are creating or using object B, the control is in your hands. After the IOC container was introduced into the software system, this situation completely changed. As shown in Figure 2, due to the addition of the IOC container, the direct connection between object A and object B was lost. Therefore, when object A runs to When object B is needed, the IOC container will actively create an object B and inject it into the location where object A is needed. Through the comparison before and after, it is not difficult to see that the process of object A becoming dependent on object B changes from active behavior to passive behavior, and the control rights are reversed. This is the origin of the name "inversion of control".


Instance

##Reflection instance code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StudentDAL
{
    public class Student
    {
        //属性
        public string Name{get;set;}
        public int Age { get; set; }

        //无参数构造函数
        public Student()
        {
            this.Name = "无参数";
            this.Age = 0;

        }
        //有参数构造函数
        public Student(string name, int age)
        {
            this.Name = "name";
            this.Age = age;
        }

        //public带参带返回值函数
        public string PublishMethodReturn()
        {
            return string.Format("我叫"+Name+"年龄" +Age);
        }
    }
}
rrree



Running result



##IOC example code


#

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(string[] args)
        {
            //使用 Assembly 定义和加载程序集,
            //加载在程序集清单中列出的模块,
            //以及从此程序集中查找类型并创建该类型的实例.
            //获取程序集
            Assembly assembly = Assembly.Load("StudentDAL");
            //从程序及获取指定对象类型
            Type type = assembly.GetType("StudentDAL.Student");
            var instance = assembly.CreateInstance("StudentDAL.Student");
            //为学生类的属性赋值
            type.GetProperty("Name").SetValue(instance, "shx", null);
            type.GetProperty("Age").SetValue(instance, 18, null);
            //获取Student类的方法
            var method = type.GetMethod("PublishMethodReturn");
            //调用Student类的成员方法PublishMethodReturn
            var S= method.Invoke(instance, null);
          
            Console.WriteLine(S);
            Console.Read();
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ITOO.IOC.IDAL
{
    public interface IUserDal
    {
        void HelloWord();
    }
}
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");
        }
    }
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ITOO.IOC.IBLL
{
    public interface IUserBll
    {
        void HelloWord();
    }
}
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从IOC容器中拿到D层的类的对象实例
            IUserDal iuser = SpringHelper.GetObject("User");
            iuser.HelloWord();
        }
    }
}


operation result



The above is the content of reflection and IOC. For more related content, please pay attention to the PHP Chinese website (m.sbmmt.com)!


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn