反射与IOC

黄舟
黄舟 原创
2017-02-23 09:46:48 1639浏览

反射

反射机制是在运行状态中,对于任意一个类,都能够知道这个类的所有属性和方法;对于任意一个对象,都能够调用它的任意一个方法;这种动态获取的以及动态调用对象的方法的功能称为反射机制。反射机制动态获取方法并使用方法和自己直接创建一个类的对象去直接调用时完全不一样的。比如一个类里面有一个属性为private的属性或者方法,我们是不能直接去调用的,但是可以使用反射机制去动态调用。

IOC

IOC最大的好处是把对象生成放在了XML里定义,所以当我们需要换一个实现子类将会变成很简单(一般这样的对象都是实现于某种接口的),只要修改XML就可以了,这样我们甚至可以实现对象的热插拨(有点象USB接口和SCSI硬盘了)。在不适用IOC之前一个对象如果依赖于另一个对象(后面我们简称依赖对象和被依赖对象),我们要在依赖对象中实例化一个被依赖对象,这样才能调用被依赖对象中的方法。显然这样耦合度比较高,不符合我们编程的原则。因此这时候我们就会引入一个第三方对象,它负责给依赖对象直接输送一个被依赖对象,降低二者之间的耦合性。下图是加入IOC容器前后,系统中对象耦合度的对比


软件系统在没有引入IOC容器之前,如图1所示,对象A依赖于对象B,那么对象A在初始化或者运行到某一点的时候,自己必须主动去创建对象B或者使用已经创建的对象B。无论是创建还是使用对象B,控制权都在自己手上。
软件系统在引入IOC容器之后,这种情形就完全改变了,如图2所示,由于IOC容器的加入,对象A与对象B之间失去了直接联系,所以,当对象A运行到需要对象B的时候,IOC容器会主动创建一个对象B注入到对象A需要的地方。
通过前后的对比,我们不难看出来:对象A获得依赖对象B的过程,由主动行为变为了被动行为,控制权颠倒过来了,这就是“控制反转”这个名称的由来。

实例

反射实例代码


<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
    {
        //属性
        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);
        }
    }
}</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(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();
        }
    }
}</strong></span>


运行结果



IOC实例代码



<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从IOC容器中拿到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)
        {
            //客户端通过底层封装的SpringHelper从IOC容器中根据B层类的对象的id拿到UserBll类的实例
            IUserBll iuserbll = SpringHelper.GetObject<IUserBll>("UserBll");
            //调用UserBll类的方法
            iuserbll.HelloWord();
            Console.Read();
        }
    }
}
</strong></span>


运行结果



以上就是反射与IOC的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!



声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn核实处理。