首頁  >  文章  >  後端開發  >  反射與IOC

反射與IOC

黄舟
黄舟原創
2017-02-23 09:46:482142瀏覽

反射

  反射機制是在運作狀態中,對於任一類,都能夠知道這個類別的所有屬性和方法;對於任意一個對象,都能夠呼叫它的任意一個方法;這種動態取得的以及動態呼叫對象的方法的功能稱為反射機制。反射機制動態獲取方法並使用方法和自己直接創建一個類別的物件去直接呼叫時完全不一樣的。例如一個類別裡面有一個屬性為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中文網(www.php.cn)! ###############################
陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn