using System;
using System.Reflection;
namespace Easy4net.Common
{
///
/// 实体类反射帮助类
///
public static class ReflectionHelper
{
///
/// 根据输入类型获取对应的成员属性集合
///
///
///
public static PropertyInfo[] GetProperties(Type type)
{
return type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
}
///
/// 根据输入类型获取对应的字段属性集合
///
///
///
public static FieldInfo[] GetFields(Type type)
{
return type.GetFields(BindingFlags.Public | BindingFlags.Instance);
}
#region 快速执行方法
///
/// 快速执行Method
///
///
///
///
///
public static object FastMethodInvoke(object obj, MethodInfo method, params object[] parameters)
{
return DynamicCalls.GetMethodInvoker(method)(obj, parameters);
}
///
/// 快速实例化一个T
///
///
///
public static T Create()
{
return (T)Create(typeof(T))();
}
///
/// 快速实例化一个FastCreateInstanceHandler
///
///
///
public static FastCreateInstanceHandler Create(Type type)
{
return DynamicCalls.GetInstanceCreator(type);
}
#endregion
#region 设置属性值
///
/// 设置属性值
///
///
///
///
public static void SetPropertyValue(object obj, PropertyInfo property, object value)
{
if (property.CanWrite)
{
var propertySetter = DynamicCalls.GetPropertySetter(property);
value = TypeUtils.ConvertForType(value, property.PropertyType);
if (value != null)
{
propertySetter(obj, value);
}
}
}
///
/// 设置属性值
///
///
///
///
public static void SetPropertyValue(object obj, string propertyName, object value)
{
SetPropertyValue(obj.GetType(), obj, propertyName, value);
}
///
/// 设置属性值
///
///
///
///
///
public static void SetPropertyValue(Type type, object obj, string propertyName, object value)
{
PropertyInfo property = type.GetProperty(propertyName);
if (property != null)
{
SetPropertyValue(obj, property, value);
}
}
///
/// 获取属性值
///
///
///
///
///
public static object GetPropertyValue(T entity, string propertyName)
{
PropertyInfo property = entity.GetType().GetProperty(propertyName);
if (property != null)
{
return property.GetValue(entity, null);
}
return null;
}
///
/// 获取属性值
///
///
///
///
///
public static object GetPropertyValue(T entity, PropertyInfo property)
{
if (property != null)
{
return property.GetValue(entity, null);
}
return null;
}
///
/// 获取属性类型
///
///
///
///
public static Type GetPropertyType(Type classType, string propertyName)
{
PropertyInfo property = classType.GetProperty(propertyName);
if (property != null)
{
return property.PropertyType;
}
return null;
}
#endregion
/*public static void SetPropertyValue(Object obj, PropertyInfo property, Object value)
{
//创建Set委托
SetHandler setter = DynamicMethodCompiler.CreateSetHandler(obj.GetType(),property);
//先获取该私有成员的数据类型
Type type = property.PropertyType;
//通过数据类型转换
value = TypeUtils.ConvertForType(value, type);
//将值设置到对象中
setter(obj, value);
}
public static Object GetPropertyValue(Object obj, PropertyInfo property)
{
//创建Set委托
GetHandler getter = DynamicMethodCompiler.CreateGetHandler(obj.GetType(), property);
//获取属性值
return getter(obj);
}
public static void SetFieldValue(Object obj, FieldInfo field, Object value)
{
//创建Set委托
SetHandler setter = DynamicMethodCompiler.CreateSetHandler(obj.GetType(), field);
//先获取该私有成员的数据类型
Type type = field.FieldType;
//通过数据类型转换
value = TypeUtils.ConvertForType(value, type);
//将值设置到对象中
setter(obj, value);
}
public static Object GetFieldValue(Object obj, FieldInfo field)
{
//创建Set委托
GetHandler getter = DynamicMethodCompiler.CreateGetHandler(obj.GetType(), field);
//获取字段值
return getter(obj);
}*/
}
}