您的位置:首页 > 编程语言 > C#

C#反射(Reflection)的应用

2015-08-10 17:13 651 查看
 反射(Reflection)是C#里很重要的一个特性,其它语言也有这个特性,比如JAVA。反射这个特性是很实用的,这个到底有多实用呢,我也说不清,如果使用过struts, hibernate, spring等等这些框架的话,便会知道反射这个特性是多么的强大了。好像我列出的都是JAVA的框架,.NET的框架我不了解,有没有我都不知道。但在我接触过的那些框架中,没有一个框架是不使用反射的,没有反射特性的语言除外。

  最近比较累,我就不多说了,直接看代码吧。

  这是Model程序集中的一个类:



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);
}
}
}


  这是反射的操作:



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);
}
}
}
}


  下面是测试类:



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");
}
}
}


  程序的运行结果如下图:



  

  上面只是介绍了反射的应用而已,下面介绍反射在数据库操作层的应用。好晚了,想睡觉了,直接看代码吧.



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的简化版是完全没问题的。实在不好意思,因为今天学习得比较累了,没什么心情作详细的解说,下面把我这个小练习的源码也共享给大家,有需要的就下载吧,代码大部分都有解释的。

  http://files.cnblogs.com/reallypride/Pride.DBHelper.rar
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: