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程序集引用,加载了程序集的反射调用方法一(二) Assembly asb = System.Reflection.Assembly.Load("CConfigFile");//加载程序集,需要程序集名称,通过添加引用,相当于告诉了要加载程序集的绝对路径 Business.CFileSection cfs = asb.CreateInstance("Business.CFileSection") as Business.CFileSection;//创建实例,需要命名空间+类名 this.txtInfo.Text = cfs.GetMessage("zhuangyq", 1); #endregion
#region 方法一(二) //需要添加CConfigFile程序集引用,加载了程序集的反射调用方法一 Assembly asb = System.Reflection.Assembly.Load("CConfigFile");//加载程序集,需要程序集名称,通过添加引用,相当于告诉了要加载程序集的绝对路径 IChange cfs = asb.CreateInstance("Business.CFileSection") as IChange;//创建实例,需要命名空间+类名 //这里通过使用接口比上面通过使用继承该接口的类耦合度降了些 this.txtInfo.Text = cfs.GetMessage("zhuangyq", 1); #endregion
#region 方法二 //无需添加CConfigFile程序集引用,加载了程序集的反射调用方法二 相对第一种耦合性有所降低 Assembly asb = System.Reflection.Assembly.LoadFile(@"F:\WinTest\ReflecTionAssembly\CConfigFile\bin\Debug\CConfigFile.dll");//加载程序集 IChange cfs = asb.CreateInstance("Business.CFileSection") as IChange;//创建实例,需要命名空间+类名 this.txtInfo.Text = cfs.GetMessage("zhuangyq", 2); #endregion
#region 方法三 //无需添加CConfigFile程序集引用,加载了程序集的反射调用方法三 相对第二种耦合性又降低 Assembly asb = System.Reflection.Assembly.LoadFrom(@"F:\WinTest\ReflecTionAssembly\CConfigFile\bin\Debug\CConfigFile.dll"); object obj = asb.CreateInstance("Business.CFileSection");//创建实例,需要命名空间+类名
Type type = asb.GetType("Business.CFileSection");//使用反射的方式获取类型 //还可以使用以下方法获取类型 // Type type = obj.GetType(); //因为已经成功创建了对象实例
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 方法三(二) //加载了程序集的反射调用方法三 相对第二种耦合性又降低 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程序集下也是正确,因为显示确认对象了(正确) 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");//如果是当前程序集的类,可以用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");//如果是当前程序集的类,可以用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! 无参数"; } public string GetFile(string Info) { return this.GetFile() + "1个参数:" + Info; } public string GetFile(string Info, int time) { return "Hello World!" + "2个参数:" + 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! 无参数"; } public string GetFile(string Info) { return this.GetFile() + "1个参数:" + Info; }
public string GetFile(string Info, int time) { return "Hello World!" + "2个参数:" + 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! 无参数"; } public string GetMessage(string Info) { return this.GetMessage() + "1个参数:" + Info; }
public string GetMessage(string Info, int time) { return "Hello World!" + "2个参数:" + Info + "和" + time; } #endregion }
|
|