反射
反射机制是在运转状况中,关于恣意一个类,都能够晓得这个类的一切属性和要领;关于恣意一个对象,都能够挪用它的恣意一个要领;这类动态猎取的以及动态挪用对象的要领的功用称为反射机制。反射机制动态猎取要领并运用要领和本身直接建立一个类的对象去直接挪用时完整不一样的。比方一个类内里有一个属性为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的内容,更多相关内容请关注ki4网(www.ki4.cn)!