反射测试笔记,主要测试的重点有
a.通过反射得到类中的‘属性’名(以前我特别想得到属性的名称或者变量名)
b.两种反射的应用,一个是通过类型反射,一个是通过‘程序集名’反射
c.调用反射类的方法(包括实例方法、静态方法)
d.创建一个反射类的新实例
--------------------------------------------------------------------------------------------
1.用于反射的测试类
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;namespace Attribute.utility.TestField
{
public class FieldCls1
{
public string Fele11 = "";
public string Fele12 = "";
public string Fele13 = ""; public string Att10 { set; get; }
public string Att11 { set; get; }
public string Att12 { set; get; }
public string Att13 { set; get; } public void Fun11() { }
public void Fun12() { }
public void Fun13() { } public string Fun14() { return "This is Fun14()"; }
public string Fun15() { return "This is Fun15()"; }
public string Fun16() { return "This is Fun16()"; }
}
}
--------------------------------------------------------------------------------------------
2. 反射的一些测试方法
#region 方法
/// <summary>
/// 显示信息到窗体控件
/// </summary>
/// <param name="msg"></param>
private void ShowMsg(object msg)
{
if (msg == null || msg == DBNull.Value)
{
ShowMsg("msg is null");
return;
}
ShowMsg(msg.ToString());
}
/// <summary>
/// 显示信息到窗体控件(lsbMsg 为 winfrom 中的 ListBox 控件)
/// </summary>
/// <param name="msg"></param>
private void ShowMsg(string msg)
{
this.lsbMsg.Items.Add(msg);
}
/// <summary>
/// 显示指定类型的成员信息
/// </summary>
/// <param name="t"></param>
private void ShowField(Type t)
{
if (t == null)
return; //获得当前类型的公共字段名
System.Reflection.FieldInfo[] fields = t.GetFields();
this.ShowMsg("----- GetFields() -----");
foreach (System.Reflection.FieldInfo f in fields)
{
this.ShowMsg(f.Name);
} //获得当前类型的公共方法名
System.Reflection.MethodInfo[] methods = t.GetMethods();
this.ShowMsg("----- GetMethods() -----");
foreach (System.Reflection.MethodInfo f in methods)
{
this.ShowMsg(f.Name);
} //获得当前类型的公共属性名
System.Reflection.PropertyInfo[] properts = t.GetProperties();
this.ShowMsg("----- GetProperties() -----");
foreach (System.Reflection.PropertyInfo f in properts)
{
this.ShowMsg(f.Name);
} Attribute.utility.TestField.FieldCls1 fCls =
new Attribute.utility.TestField.FieldCls1();
//因为一个类不仅仅包含了自定的方法,还包含了一些基类的一些方法
//这里只是通过 linq 把自定义的方法查了出来
System.Reflection.MethodInfo[] customMethods =
methods.Where(o =>
{
if(o.Name.StartsWith("Fun1"))
return true; return false;
}).ToArray();
//调用反射后的方法
this.ShowMsg("----- Invoke GetMethods() -----");
object obj = t.Assembly.CreateInstance(t.FullName);
foreach (System.Reflection.MethodInfo f in customMethods)
{
this.ShowMsg(
string.Format(
"{0},returns = {1}"
, f.Name
, f.Invoke(obj, null) //注意:obj 为反射类的实例类,如果调用的方法为静态的这个参数则为 null
));
} //调用反射后类的实例化方方法
this.ShowMsg("----- Execute Instance Method -----");
//object obj = t.Assembly.CreateInstance(t.FullName, true);
if (obj is Attribute.utility.TestField.FieldCls1)
{
Attribute.utility.TestField.FieldCls1 cls = (Attribute.utility.TestField.FieldCls1)obj;
this.ShowMsg(string.Format("{0},returns = {1}", cls.ToString(),cls.Fun14()));
this.ShowMsg(string.Format("{0},returns = {1}", cls.ToString(),cls.Fun15()));
this.ShowMsg(string.Format("{0},returns = {1}", cls.ToString(),cls.Fun16()));
} }
#endregion