C#反射(Reflection)的应用

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

  http://files.cnblogs.com/reallypride/Pride.DBHelper.rar

转载于:https://www.cnblogs.com/reallypride/archive/2008/09/07/1285875.html

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值