这篇文章的目的是介绍这样一种方式,就是在写一个函数的时候,传递的参数是object类型的,在这个函数里面想访问这个参数对象的某一属性值,我们知道这个属性值的name,但是一般情况下,object对象是没法获取具体属性的值的,所以用下面的方式可以获取。此文章为转载,原文在:http://lsyyxcn.blog.163.com/blog/static/22740531201002792629559/
/// <summary> /// 反射操作辅助类 /// </summary> public sealed class ReflectionUtil { private ReflectionUtil() { } private static BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static; /**//// <summary> /// 执行某个方法 /// </summary> /// <param name="obj">指定的对象</param> /// <param name="methodName">对象方法名称</param> /// <param name="args">参数</param> /// <returns></returns> public static object InvokeMethod(object obj, string methodName, object[] args) { object objResult = null; Type type = obj.GetType(); objResult = type.InvokeMember(methodName, bindingFlags | BindingFlags.InvokeMethod, null, obj, args); return objResult; } /**//// <summary> /// 设置对象字段的值 /// </summary> public static void SetField(object obj, string name, object value) { FieldInfo fieldInfo = obj.GetType().GetField(name, bindingFlags); object objValue = Convert.ChangeType(value, fieldInfo.FieldType); fieldInfo.SetValue(objValue, value); } /**//// <summary> /// 获取对象字段的值 /// </summary> public static object GetField(object obj, string name) { FieldInfo fieldInfo = obj.GetType().GetField(name, bindingFlags); return fieldInfo.GetValue(obj); } /**//// <summary> /// 设置对象属性的值 /// </summary> public static void SetProperty(object obj, string name, object value) { PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bindingFlags); object objValue = Convert.ChangeType(value, propertyInfo.PropertyType); propertyInfo.SetValue(obj, objValue, null); } /**//// <summary> /// 获取对象属性的值 /// </summary> public static object GetProperty(object obj, string name) { PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bindingFlags); return propertyInfo.GetValue(obj, null); } /**//// <summary> /// 获取对象属性信息(组装成字符串输出) /// </summary> public static string GetProperties(object obj) { StringBuilder strBuilder = new StringBuilder(); PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bindingFlags); foreach (PropertyInfo property in propertyInfos) { strBuilder.Append(property.Name); strBuilder.Append(":"); strBuilder.Append(property.GetValue(obj, null)); strBuilder.Append(" "); } return strBuilder.ToString(); } } 反射操作辅助类ReflectionUtil测试代码: public class TestReflectionUtil { public static string Execute() { string result = string.Empty; result += "使用ReflectionUtil反射操作辅助类:" + " "; try { Person person = new Person(); person.Name = "wuhuacong"; person.Age = 20; result += DecriptPerson(person); person.Name = "Wade Wu"; person.Age = 99; result += DecriptPerson(person); } catch (Exception ex) { result += string.Format("发生错误:{0}! ", ex.Message); } return result; } public static string DecriptPerson(Person person) { string result = string.Empty; result += "name:" + ReflectionUtil.GetField(person, "name") + " "; result += "age" + ReflectionUtil.GetField(person, "age") + " "; result += "Name:" + ReflectionUtil.GetProperty(person, "Name") + " "; result += "Age:" + ReflectionUtil.GetProperty(person, "Age") + " "; result += "Say:" + ReflectionUtil.InvokeMethod(person, "Say", new object[] {}) + " "; result += "操作完成! "; return result; } }
/// <summary>
/// 反射操作辅助类
/// </summary>
public sealed class ReflectionUtil
{
private ReflectionUtil()
{
}
private static BindingFlags bindingFlags = BindingFlags.DeclaredOnly | BindingFlags.Public |
BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static;
/**//// <summary>
/// 执行某个方法
/// </summary>
/// <param name="obj">指定的对象</param>
/// <param name="methodName">对象方法名称</param>
/// <param name="args">参数</param>
/// <returns></returns>
public static object InvokeMethod(object obj, string methodName, object[] args)
{
object objResult = null;
Type type = obj.GetType();
objResult = type.InvokeMember(methodName, bindingFlags | BindingFlags.InvokeMethod, null, obj, args);
return objResult;
}
/**//// <summary>
/// 设置对象字段的值
/// </summary>
public static void SetField(object obj, string name, object value)
{
FieldInfo fieldInfo = obj.GetType().GetField(name, bindingFlags);
object objValue = Convert.ChangeType(value, fieldInfo.FieldType);
fieldInfo.SetValue(objValue, value);
}
/**//// <summary>
/// 获取对象字段的值
/// </summary>
public static object GetField(object obj, string name)
{
FieldInfo fieldInfo = obj.GetType().GetField(name, bindingFlags);
return fieldInfo.GetValue(obj);
}
/**//// <summary>
/// 设置对象属性的值
/// </summary>
public static void SetProperty(object obj, string name, object value)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bindingFlags);
object objValue = Convert.ChangeType(value, propertyInfo.PropertyType);
propertyInfo.SetValue(obj, objValue, null);
}
/**//// <summary>
/// 获取对象属性的值
/// </summary>
public static object GetProperty(object obj, string name)
{
PropertyInfo propertyInfo = obj.GetType().GetProperty(name, bindingFlags);
return propertyInfo.GetValue(obj, null);
}
/**//// <summary>
/// 获取对象属性信息(组装成字符串输出)
/// </summary>
public static string GetProperties(object obj)
{
StringBuilder strBuilder = new StringBuilder();
PropertyInfo[] propertyInfos = obj.GetType().GetProperties(bindingFlags);
foreach (PropertyInfo property in propertyInfos)
{
strBuilder.Append(property.Name);
strBuilder.Append(":");
strBuilder.Append(property.GetValue(obj, null));
strBuilder.Append("
");
}
return strBuilder.ToString();
}
}
反射操作辅助类ReflectionUtil测试代码:
public class TestReflectionUtil
{
public static string Execute()
{
string result = string.Empty;
result += "使用ReflectionUtil反射操作辅助类:" + "
";
try
{
Person person = new Person();
person.Name = "wuhuacong";
person.Age = 20;
result += DecriptPerson(person);
person.Name = "Wade Wu";
person.Age = 99;
result += DecriptPerson(person);
}
catch (Exception ex)
{
result += string.Format("发生错误:{0}!
", ex.Message);
}
return result;
}
public static string DecriptPerson(Person person)
{
string result = string.Empty;
result += "name:" + ReflectionUtil.GetField(person, "name") + "
";
result += "age" + ReflectionUtil.GetField(person, "age") + "
";
result += "Name:" + ReflectionUtil.GetProperty(person, "Name") + "
";
result += "Age:" + ReflectionUtil.GetProperty(person, "Age") + "
";
result += "Say:" + ReflectionUtil.InvokeMethod(person, "Say", new object[] {}) + " ";
result += "操作完成! ";
return result;
}
}