1.主窗体下的代码
namespace TestReflection
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void simpleButton1_Click(object sender, EventArgs e)
{
UseAllMethod();
}
/// <summary>
/// 遍历查看类下的所有方法的信息
/// </summary>
private void UseAllMethod()
{
try
{
//// 获取程序集中所有定义的类
Type[] types = Assembly.Load("TestReflection").GetTypes();
//// 遍历类
for (int i = 0; i < types.Length; i++)
{
if (types[i].Name.Equals("Test"))
{
//// 获取类下的所有方法
MethodInfo[] methods = types[i].GetMethods();
//// 遍历类下的方法
for (int j = 0; j < methods.Length; j++)
{
listBoxControl1.Items.Add(string.Format("方法名{0}:{1},返回值的类型:{2},是静态方法{3}",
j + 1, methods[j].Name, methods[j].ReturnType, methods[j].IsStatic));
//// 获取方法下的参数
ParameterInfo[] parameters = methods[j].GetParameters();
//// 遍历方法下的参数
for (int k = 0; k < parameters.Length;k++ )
{
listBoxControl1.Items.Add(string.Format("参数{0}:{1} {2},签名位置:{3},是否输出参数{4}",
k+1,parameters[k].ParameterType,parameters[k].Name,parameters[k].Position,parameters[k].IsOut));
}
listBoxControl1.Items.Add("");
}
}
}
}
catch
{
}
}
}
}
namespace TestReflection
{
class Test
{
public void SayChinese(Form1 frm,string Text)
{
frm.labelControl1.Text = Text;
}
public void SayEnglish(Form1 frm, string Text)
{
frm.labelControl1.Text = Text;
}
public static void SayStatic(Form1 frm, string Text)
{
frm.labelControl1.Text = Text;
}
}
}
3.运行结果