反射
反射機制是在運作狀態中,對於任一類,都能夠知道這個類別的所有屬性和方法;對於任意一個對象,都能夠呼叫它的任意一個方法;這種動態取得的以及動態呼叫對象的方法的功能稱為反射機制。反射機制動態獲取方法並使用方法和自己直接創建一個類別的物件去直接呼叫時完全不一樣的。例如一個類別裡面有一個屬性為private的屬性或是方法,我們是不能直接去呼叫的,但是可以使用反射機制去動態呼叫。
IOC
# IOC最大的優點就是把物件產生放在了XML裡定義,所以當我們需要換一個實作子類別將會變成很簡單(一般這樣的物件都是實作於某種介面的),只要修改XML就可以了,這樣我們甚至可以實現物件的熱插撥(有點象USB介面和SCSI硬碟了)。在不適用IOC之前一個物件如果依賴另一個物件(後面我們簡稱依賴物件和被依賴物件),我們要在依賴物件中實例化一個被依賴對象,這樣才能呼叫被依賴物件中的方法。顯然這樣耦合度比較高,不符合我們程式設計的原則。因此這時候我們就會引入一個第三方對象,它負責直接傳遞一個依賴對象,降低二者之間的耦合性給依賴對象。下圖是加入IOC容器前後,系統中物件耦合度的比較
# 軟體系統在沒有引入IOC容器之前,如圖1所示,物件A依賴物件B,那麼物件A在初始化或運作到某一點的時候,自己必須主動去創建物件B或使用已經建立的物件B。無論是創建還是使用物件B,控制權都在自己手上。
透過前後的對比,我們不難看出來:物件A獲得依賴物件B的過程,由主動行為變為了被動行為,控制權顛倒過來了,這就是「控制反轉」這個名稱的由來。
實例
#反射實例程式碼
<span style="max-width:90%"><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)! ###############################

C#和.NET的結合為開發者提供了強大的編程環境。 1)C#支持多態性和異步編程,2).NET提供跨平台能力和並發處理機制,這使得它們在桌面、Web和移動應用開發中廣泛應用。

.NETFramework是一個軟件框架,C#是一種編程語言。 1..NETFramework提供庫和服務,支持桌面、Web和移動應用開發。 2.C#設計用於.NETFramework,支持現代編程功能。 3..NETFramework通過CLR管理代碼執行,C#代碼編譯成IL後由CLR運行。 4.使用.NETFramework可快速開發應用,C#提供如LINQ的高級功能。 5.常見錯誤包括類型轉換和異步編程死鎖,調試需用VisualStudio工具。

C#是一種由微軟開發的現代、面向對象的編程語言,.NET是微軟提供的開發框架。 C#結合了C 的性能和Java的簡潔性,適用於構建各種應用程序。 .NET框架支持多種語言,提供垃圾回收機制,簡化內存管理。

C#和.NET運行時緊密合作,賦予開發者高效、強大且跨平台的開發能力。 1)C#是一種類型安全且面向對象的編程語言,旨在與.NET框架無縫集成。 2).NET運行時管理C#代碼的執行,提供垃圾回收、類型安全等服務,確保高效和跨平台運行。

要開始C#.NET開發,你需要:1.了解C#的基礎知識和.NET框架的核心概念;2.掌握變量、數據類型、控制結構、函數和類的基本概念;3.學習C#的高級特性,如LINQ和異步編程;4.熟悉常見錯誤的調試技巧和性能優化方法。通過這些步驟,你可以逐步深入C#.NET的世界,並編寫高效的應用程序。

C#和.NET的關係是密不可分的,但它們不是一回事。 C#是一門編程語言,而.NET是一個開發平台。 C#用於編寫代碼,編譯成.NET的中間語言(IL),由.NET運行時(CLR)執行。

C#.NET依然重要,因為它提供了強大的工具和庫,支持多種應用開發。 1)C#結合.NET框架,使開發高效便捷。 2)C#的類型安全和垃圾回收機制增強了其優勢。 3).NET提供跨平台運行環境和豐富的API,提升了開發靈活性。

C#.NETisversatileforbothwebanddesktopdevelopment.1)Forweb,useASP.NETfordynamicapplications.2)Fordesktop,employWindowsFormsorWPFforrichinterfaces.3)UseXamarinforcross-platformdevelopment,enablingcodesharingacrossWindows,macOS,Linux,andmobiledevices.


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

PhpStorm Mac 版本
最新(2018.2.1 )專業的PHP整合開發工具

SecLists
SecLists是最終安全測試人員的伙伴。它是一個包含各種類型清單的集合,這些清單在安全評估過程中經常使用,而且都在一個地方。 SecLists透過方便地提供安全測試人員可能需要的所有列表,幫助提高安全測試的效率和生產力。清單類型包括使用者名稱、密碼、URL、模糊測試有效載荷、敏感資料模式、Web shell等等。測試人員只需將此儲存庫拉到新的測試機上,他就可以存取所需的每種類型的清單。

禪工作室 13.0.1
強大的PHP整合開發環境

記事本++7.3.1
好用且免費的程式碼編輯器

SAP NetWeaver Server Adapter for Eclipse
將Eclipse與SAP NetWeaver應用伺服器整合。