反射(Reflection)是C#裡很重要的一個特性,其它語言也有這個特性,比如JAVA。反射這個特性是很實用的,這個到底有多實用呢,我也說不清,如果使用過struts, hibernate, spring等等這些框架的話,便會知道反射這個特性是多麼的強大了。好像我列出的都是JAVA的框架,.NET的框架我不了解,有沒有我都不知道。但在我接觸過的那些框架中,沒有一個框架是不使用反射的,沒有反射特性的語言除外。
最近比較累,我就不多說了,直接看代碼吧。
這是Model程序集中的一個類:
Code
using System;
using System.Collections.Generic;
using System.Text;
namespace Model
{
public class UserInfo
{
private int userId;
public int UserId
{
get { return userId; }
set { userId = value; }
}
private string userName;
public string UserName
{
get { return userName; }
set { userName = value; }
}
public void Show()
{
Console.WriteLine("UserId:" + userId + ", UserName:" + userName);
}
}
}
這是反射的操作:
Code
using System;
using System.Collections.Generic;
using System.Text;
using System.Reflection;
namespace ObjectLoader
{
public class ShowObject
{
//加載程序集
private Assembly assembly = Assembly.Load("Model");
/// <summary>
/// 實例化類,要包含它的命名空間
/// </summary>
/// <param name="objName">類名</param>
/// <returns></returns>
public object LoadObject(string objName)
{
return assembly.CreateInstance("Model." + objName);
}
/// <summary>
/// 返回所有的公共屬性
/// </summary>
/// <param name="obj"></param>
/// <returns></returns>
public PropertyInfo[] GetPropertys(object obj)
{
Type type = obj.GetType();
PropertyInfo[] infos = type.GetProperties();
return infos;
}
/// <summary>
/// 設置實例的指定屬性值
/// </summary>
/// <param name="obj">實例</param>
/// <param name="property">屬性名</param>
/// <param name="value">值</param>
public void SetPropertyValue(object obj, string property, object value)
{
Type type = obj.GetType();
PropertyInfo info = type.GetProperty(property);
if (info != null)
{
info.SetValue(obj, value, null);
}
}
/// <summary>
/// 返回指定屬性值
/// </summary>
/// <param name="obj">實例</param>
/// <param name="property">屬性名</param>
/// <returns></returns>
public object GetPropertyValue(object obj, string property)
{
Type type = obj.GetType();
PropertyInfo info = type.GetProperty(property);
if (info == null)
{
return null;
}
return info.GetValue(obj, null);
}
/// <summary>
/// 執行實例的指定方法
/// </summary>
/// <param name="obj"></param>
/// <param name="methodName">方法名</param>
public void ExecuteMethod(object obj, string methodName)
{
Type type = obj.GetType();
MethodInfo info = type.GetMethod(methodName);
if (info != null)
{
info.Invoke(obj, null);
}
}
}
}
下面是測試類:
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");
}
}
}
程序的運行結果如下圖:
上面只是介紹了反射的應用而已,下面介紹反射在數據庫操作層的應用。好晚了,想睡覺了,直接看代碼吧.
Code
using System;
using System.Collections;
using System.Text;
using System.Data;
using ObjectLoader;
namespace DataTableAOP
{
public class ObjectList
{
/// <summary>
/// 將DataTable轉換成ArrayList
/// </summary>
/// <param name="dataTable"></param>
/// <param name="name">類名</param>
/// <returns></returns>
public static ArrayList GetObjectList(DataTable dataTable, string name)
{
ShowObject show = new ShowObject();
DataRowCollection rows = dataTable.Rows;
ArrayList list = new ArrayList();
foreach (DataRow row in rows)
{
//使用反射生成新的實例
object obj = show.LoadObject("UserName");
foreach (DataColumn column in dataTable.Columns)
{
//根據字段名設置屬性值
show.SetPropertyValue(obj, column.ColumnName, row[column.ColumnName]);
}
list.Add(obj);
}
return list;
}
}
}
各位如果有興趣的話,做個類似於hibernate的簡化版是完全沒問題的。
本文配套源碼