当前位置:
首页 > 编程开发 > Objective-C编程 >
-
c#反射Reflected的应用
制作者:剑锋冷月 单位:无忧统计网,www.51stat.net
本文示例源代码或素材下载
反射(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的简化版是完全没问题的。
下面是测试类:
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");
}
}
}
本文示例源代码或素材下载
反射(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的简化版是完全没问题的。
下面是测试类:
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");
}
}
}
最新更新
nodejs爬虫
Python正则表达式完全指南
爬取豆瓣Top250图书数据
shp 地图文件批量添加字段
爬虫小试牛刀(爬取学校通知公告)
【python基础】函数-初识函数
【python基础】函数-返回值
HTTP请求:requests模块基础使用必知必会
Python初学者友好丨详解参数传递类型
如何有效管理爬虫流量?
2个场景实例讲解GaussDB(DWS)基表统计信息估
常用的 SQL Server 关键字及其含义
动手分析SQL Server中的事务中使用的锁
openGauss内核分析:SQL by pass & 经典执行
一招教你如何高效批量导入与更新数据
天天写SQL,这些神奇的特性你知道吗?
openGauss内核分析:执行计划生成
[IM002]Navicat ODBC驱动器管理器 未发现数据
初入Sql Server 之 存储过程的简单使用
SQL Server -- 解决存储过程传入参数作为s
关于JS定时器的整理
JS中使用Promise.all控制所有的异步请求都完
js中字符串的方法
import-local执行流程与node模块路径解析流程
检测数据类型的四种方法
js中数组的方法,32种方法
前端操作方法
数据类型
window.localStorage.setItem 和 localStorage.setIte
如何完美解决前端数字计算精度丢失与数