窗体的代码:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;
using Models;
namespace test
{
public partial class Form3 : Form
{
public Form3()
{
InitializeComponent();
}
private void Form3_FormClosing(object sender, FormClosingEventArgs e)
{
if (DialogResult.OK != MessageBox.Show("你确定要退出系统", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information))
{
e.Cancel = true;
// MessageBox.Show(e.CloseReason.ToString());
}
else
{
}
}
private void button1_Click(object sender, EventArgs e)
{
this.Close();
}
private void btnOk_Click(object sender, EventArgs e)
{
//获取程序集
Assembly assembly = Assembly.Load("Models");
Assembly assembly2 = Assembly.LoadFrom("Models.dll");
//从程序集中获取指定对象类型;
Type type = assembly2.GetType("Models.User");
//使用Activator创建一个实例使用Activator创建一个实例
User user2 = Activator.CreateInstance(type) as User;
//通过对象类型创建对象实例
User user = assembly.CreateInstance("Models.User") as User;
// 调用无参构造
ConstructorInfo studentConstructor = type.GetConstructor(new Type[] { });
User user3 = studentConstructor.Invoke(new object[] { }) as User;
//调用有参构造
ConstructorInfo studentConstructor2 = type.GetConstructor(new Type[] { typeof(string) });
User user4 = studentConstructor.Invoke(new object[] { "您调用了有参构造创建了一个类实例。"}) as User;
//调用非静态方法
string returnValue = type.InvokeMember("PublicClassMethod", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Instance, null, user, new object[] { }) as string;
// 调用静态方法 .
string returnValue2 = type.InvokeMember("StaticMethod", BindingFlags.InvokeMethod | BindingFlags.Public | BindingFlags.Static, null, user, new object[] { }) as string;
// 调用私有方法 .
string returnValue3 = type.InvokeMember("PrivateClassMethod", BindingFlags.InvokeMethod | BindingFlags.NonPublic | BindingFlags.Instance, null, user, new object[] { }) as string;
//反射参数反射参数
MethodInfo parameterMethod = type.GetMethod("Show");
ParameterInfo[] paras = parameterMethod.GetParameters();
for (int i = 0; i < paras.Length; i++)
{
MessageBox.Show(string.Format("参数名称:{0},参数类型:{1},是否可选:{2},位置:{3},默认值:{4}", paras[i].Name, paras[i].ParameterType.ToString(), paras[i].IsOptional.ToString(), paras[i].Position.ToString(), paras[i].DefaultValue.ToString()));
//反射属性反射属性
type.InvokeMember("Name", BindingFlags.SetProperty | BindingFlags.Public | BindingFlags.Instance, null, user, new object[] { "您反射了一个属性" });
string returnValue4 = type.InvokeMember("Name", BindingFlags.GetProperty | BindingFlags.Public | BindingFlags.Instance, null, user, new object[] { }) as string;
//反射字段反射字段
type.InvokeMember("Field", BindingFlags.SetField | BindingFlags.Public | BindingFlags.Instance, null, user, new object[] { "您反射了一个字段" });
string returnValue5 = type.InvokeMember("Field", BindingFlags.GetField | BindingFlags.Public | BindingFlags.Instance, null, user, new object[] { }) as string;
}
}
}
}
BindingFlags用于指定你要得的的函数或者成员的类
BindingFlags.InvokeMethod指调用方法。
BindingFlags.SetProperty指设置属性
BindingFlags.GetProperty指得到属性
BindingFlags.SetField指设置字段
BindingFlags.GetField指得到字段
BindingFlags.Instance 或 BindingFlags.Static必须指定其一,以确定你要的是否是静态方法.
BindingFlags.Instance ,反射的时候会去实例成员,也就是非静态的,你的方法是非静态的,就必须必须加上这个才行。
BindingFlags.Static ,要反射静态的方法就必须加上这个。 |