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

 

posted @ 2009-11-20 17:40  任崸  阅读(486)  评论(0编辑  收藏  举报