frm_Main.cs類 using System; using System.Collections.Generic; using System.ComponentModel; using System.Da using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; using System.Reflection; using Common; using Business;
namespace ReflecTionAssembly { public partial class frm_Main : Form { public frm_Main() { InitializeComponent(); }
private void btnOpt_Click(object sender, EventArgs e) {
#region 方法一 //需要添加CConfigFile程序集引用,加載了程序集的反射調(diào)用方法一(二) Assembly asb = System.Reflection.Assembly.Load("CConfigFile");//加載程序集,需要程序集名稱,通過添加引用,相當(dāng)于告訴了要加載程序集的絕對路徑 Business.CFileSection cfs = asb.CreateInstance("Business.CFileSection") as Business.CFileSection;//創(chuàng)建實例,需要命名空間+類名 this.txtInfo.Text = cfs.GetMessage("zhuangyq", 1); #endregion
#region 方法一(二) //需要添加CConfigFile程序集引用,加載了程序集的反射調(diào)用方法一 Assembly asb = System.Reflection.Assembly.Load("CConfigFile");//加載程序集,需要程序集名稱,通過添加引用,相當(dāng)于告訴了要加載程序集的絕對路徑 IChange cfs = asb.CreateInstance("Business.CFileSection") as IChange;//創(chuàng)建實例,需要命名空間+類名 //這里通過使用接口比上面通過使用繼承該接口的類耦合度降了些 this.txtInfo.Text = cfs.GetMessage("zhuangyq", 1); #endregion
#region 方法二 //無需添加CConfigFile程序集引用,加載了程序集的反射調(diào)用方法二 相對第一種耦合性有所降低 Assembly asb = System.Reflection.Assembly.LoadFile(@"F:\WinTest\ReflecTionAssembly\CConfigFile\bin\Debug\CConfigFile.dll");//加載程序集 IChange cfs = asb.CreateInstance("Business.CFileSection") as IChange;//創(chuàng)建實例,需要命名空間+類名 this.txtInfo.Text = cfs.GetMessage("zhuangyq", 2); #endregion
#region 方法三 //無需添加CConfigFile程序集引用,加載了程序集的反射調(diào)用方法三 相對第二種耦合性又降低 Assembly asb = System.Reflection.Assembly.LoadFrom(@"F:\WinTest\ReflecTionAssembly\CConfigFile\bin\Debug\CConfigFile.dll"); object obj = asb.CreateInstance("Business.CFileSection");//創(chuàng)建實例,需要命名空間+類名
Type type = asb.GetType("Business.CFileSection");//使用反射的方式獲取類型 //還可以使用以下方法獲取類型 // Type type = obj.GetType(); //因為已經(jīng)成功創(chuàng)建了對象實例
System.Reflection.MethodInfo method = type.GetMethod("GetMessage", new Type[] { typeof(string), typeof(int) }); this.txtInfo.Text = method.Invoke(obj, new object[] { "zhuangyq", 3 }) as string; #endregion
#region 方法三(二) //加載了程序集的反射調(diào)用方法三 相對第二種耦合性又降低 Assembly asb = System.Reflection.Assembly.LoadFile(@"F:\WinTest\ReflecTionAssembly\CConfigFile\bin\Debug\CConfigFile.dll");
Type type = asb.GetType("Business.CFileSection");
object obj = System.Activator.CreateInstance(type); System.Reflection.MethodInfo method = type.GetMethod("GetMessage", new Type[] { typeof(string), typeof(int) });
this.txtInfo.Text = method.Invoke(obj, new object[] { "zhuangyq", 3 }) as string; #endregion
#region test //以下用法會報錯,type為null,即使添加了CConfigFile程序集引用也沒用 Type type = Type.GetType("Business.CFileSection"); Business.CFileSection csf = System.Activator.CreateInstance(type) as Business.CFileSection; this.txtInfo.Text = csf.GetMessage("zhuangyq", 1);
//把如上代碼改為如下代碼即可(正確) Assembly asb = System.Reflection.Assembly.Load("CConfigFile"); Type type = asb.GetType("Business.CFileSection"); Business.CFileSection csf = System.Activator.CreateInstance(type) as Business.CFileSection; this.txtInfo.Text = csf.GetMessage("zhuangyq", 1);
//如下用法即使類不在ReflecTionAssembly程序集下也是正確,因為顯示確認(rèn)對象了(正確) Type type = typeof(Business.CFileSection);//能夠正確獲取類型 Business.CFileSection csf2 = System.Activator.CreateInstance(type) as Business.CFileSection; this.txtInfo.Text = csf2.GetMessage("zhuangyq", 4); #endregion
#region test1 CServiceFile csf = new CServiceFile(); this.txtInfo.Text = csf.GetFile("zhuangyq", 1); #endregion
#region test2 //同一程序集,且相同命名空間下類的測試(正確) Type type = Type.GetType("ReflecTionAssembly.CServiceFile");//如果是當(dāng)前程序集的類,可以用Type.GetType("命名空間+類名")獲取Type CServiceFile csf = System.Activator.CreateInstance(type) as CServiceFile; this.txtInfo.Text = csf.GetFile("zhuangyq", 4);
//顯示獲取同一命名空間下的類 Type type = typeof(CServiceFile); CServiceFile csf2 = System.Activator.CreateInstance(type) as CServiceFile; this.txtInfo.Text = csf2.GetFile("zhuangyq", 5);
//以下這種方法雖然正確,但不可取 CServiceFile csf = new CServiceFile(); Type type = csf.GetType(); CServiceFile csf2 = System.Activator.CreateInstance(type) as CServiceFile; this.txtInfo.Text = csf2.GetFile("zhuangyq", 7); #endregion
#region test3 //同一程序集,但不同命名空間下類的測試(正確) Type type = Type.GetType("ModelAssembly.CModel");//如果是當(dāng)前程序集的類,可以用Type.GetType("命名空間+類名")獲取Type object obj = System.Activator.CreateInstance(type); System.Reflection.MethodInfo method = type.GetMethod("GetFile", new Type[] { typeof(string), typeof(int) }); this.txtInfo.Text = method.Invoke(obj, new object[] { "zhuangyq", 6 }) as string; #endregion } }
public class CServiceFile { public CServiceFile() { } public string GetFile() { return "Hello World! 無參數(shù)"; } public string GetFile(string Info) { return this.GetFile() + "1個參數(shù):" + Info; } public string GetFile(string Info, int time) { return "Hello World!" + "2個參數(shù):" + Info + "和" + time; } } }
CModel.cs 類 說明:和frm_Main.cs同一程序集,但不同命名空間 using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace ModelAssembly { class CModel { public string GetFile() { return "Hello World! 無參數(shù)"; } public string GetFile(string Info) { return this.GetFile() + "1個參數(shù):" + Info; }
public string GetFile(string Info, int time) { return "Hello World!" + "2個參數(shù):" + Info + "和" + time; } } }
Interface.cs 接口 using System; using System.Collections.Generic; using System.Linq; using System.Text;
namespace Common { public interface IChange { string GetMessage(); string GetMessage(string Info); string GetMessage(string Info, int time); }
}
CFileSection.cs 類 說明:程序集名稱為CConfigFile,但是類的命名空間為:Business using System; using System.Collections.Generic; using System.Linq; using System.Text; using Common;
namespace Business { public class CFileSection:IChange { public CFileSection() {
} #region IChange 成員 public string GetMessage() { return "Hello World! 無參數(shù)"; } public string GetMessage(string Info) { return this.GetMessage() + "1個參數(shù):" + Info; }
public string GetMessage(string Info, int time) { return "Hello World!" + "2個參數(shù):" + Info + "和" + time; } #endregion }
|
|