下面是測試類:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
using Model;
using ObjectLoader;
namespace ReflectionTest
{
class Program
{
static void Main(string[] args)
{
ShowObject showObj = new ShowObject();
object obj = showObj.LoadObject("UserInfo");
Console.WriteLine("實例名:" + obj.GetType().FullName);
Console.WriteLine("實例的所有公共屬性:");
PropertyInfo[] pInfos = showObj.GetPropertys(obj);
foreach (PropertyInfo info in pInfos)
{
Console.WriteLine(info.GetType() + ":" + info.Name);
}
showObj.SetPropertyValue(obj, "UserId", 1);
showObj.SetPropertyValue(obj, "UserName", "admin");
Console.WriteLine("設置屬性值後的值");
Console.WriteLine("UserId:" + showObj.GetPropertyValue(obj,"UserId"));
Console.WriteLine("UserName:" + showObj.GetPropertyValue(obj, "UserName"));
Console.WriteLine("執行實例的方法:");
showObj.ExecuteMethod(obj, "Show");
}
}
}
程序的運行結果如下圖: