c#反射帮助类

/// <summary>
 /// 属性值动态获取和赋值(get、set)
 /// </summary>
 public class PropertyUtil
 {
     /// <summary>
     /// 反射获取对象的属性值
     /// </summary>
     /// <param name="obj"></param>
     /// <param name="propertyName"></param>
     /// <returns></returns>
     public static object ReflectGetter(object obj, string propertyName)
     {
         var type = obj.GetType();
         var propertyInfo = type.GetProperty(propertyName);
         var propertyValue = propertyInfo.GetValue(obj,null);
         return propertyValue;
     }

     /// <summary>
     /// 反射设置对象的属性值
     /// </summary>
     /// <param name="obj"></param>
     /// <param name="propertyName"></param>
     /// <param name="propertyValue"></param>
     public static void ReflectSetter(object obj, string propertyName, object propertyValue)
     {
         var type = obj.GetType();
         var propertyInfo = type.GetProperty(propertyName);
         propertyInfo.SetValue(obj, propertyValue,null);
     }


     /// <summary>
     /// 表达式获取对象的属性值
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="propertyName"></param>
     /// <returns></returns>
     public static Func<T, object> ExpresionGetter<T>(string propertyName)
     {
         var type = typeof(T);
         var property = type.GetProperty(propertyName);

          对象实例
         var parameterExpression = Expression.Parameter(typeof(object), "obj");

          转换参数为真实类型
         var unaryExpression = Expression.Convert(parameterExpression, type);

          调用获取属性的方法
         var callMethod = Expression.Call(unaryExpression, property.GetGetMethod());
         var expression = Expression.Lambda<Func<T, object>>(callMethod, parameterExpression);

         return expression.Compile();
     }


     /// <summary>
     /// 表达式设置对象的属性值
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="propertyName"></param>
     /// <returns></returns>
     public static Action<T, object> ExpresionSetter<T>(string propertyName)
     {
         var type = typeof(T);
         var property = type.GetProperty(propertyName);

         var objectParameterExpression = Expression.Parameter(typeof(object), "obj");
         var objectUnaryExpression = Expression.Convert(objectParameterExpression, type);

         var valueParameterExpression = Expression.Parameter(typeof(object), "val");
         var valueUnaryExpression = Expression.Convert(valueParameterExpression, property.PropertyType);

          调用给属性赋值的方法
         var body = Expression.Call(objectUnaryExpression, property.GetSetMethod(), valueUnaryExpression);
         var expression = Expression.Lambda<Action<T, object>>(body, objectParameterExpression, valueParameterExpression);

         return expression.Compile();
     }

     /// <summary>
     /// Emit获取对象的属性值
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="propertyName"></param>
     /// <returns></returns>
     public static Func<T, object> EmitGetter<T>(string propertyName)
     {
         var type = typeof(T);
         var dynamicMethod = new DynamicMethod("get_" + propertyName, typeof(object), new[] { type }, type);
         var iLGenerator = dynamicMethod.GetILGenerator();
         iLGenerator.Emit(OpCodes.Ldarg_0);

         var property = type.GetProperty(propertyName);
         iLGenerator.Emit(OpCodes.Callvirt, property.GetGetMethod());

     // 如果是值类型,装箱 如果是引用类型,转换
     iLGenerator.Emit(property.PropertyType.IsValueType ? OpCodes.Box : OpCodes.Castclass,
                      property.PropertyType);

     iLGenerator.Emit(OpCodes.Ret);
         return dynamicMethod.CreateDelegate(typeof(Func<T, object>)) as Func<T, object>;
     }

     /// <summary>
     /// Emit设置对象的属性值
     /// </summary>
     /// <typeparam name="T"></typeparam>
     /// <param name="propertyName"></param>
     /// <returns></returns>
     public static Action<T, object> EmitSetter<T>(string propertyName)
     {
         var type = typeof(T);

         var dynamicMethod = new DynamicMethod("EmitCallable", null, new[] { type, typeof(object) }, type.Module);
         var iLGenerator = dynamicMethod.GetILGenerator();


         var callMethod = type.GetMethod("set_" + propertyName, BindingFlags.Instance | BindingFlags.IgnoreCase | BindingFlags.Public);
         var parameterInfo = callMethod.GetParameters()[0];
         var local = iLGenerator.DeclareLocal(parameterInfo.ParameterType, true);


         iLGenerator.Emit(OpCodes.Ldarg_1);
     // 如果是值类型,拆箱 如果是引用类型,转换
     iLGenerator.Emit(parameterInfo.ParameterType.IsValueType ? OpCodes.Unbox_Any : OpCodes.Castclass,
                      parameterInfo.ParameterType);


     iLGenerator.Emit(OpCodes.Stloc, local);
         iLGenerator.Emit(OpCodes.Ldarg_0);
         iLGenerator.Emit(OpCodes.Ldloc, local);


         iLGenerator.EmitCall(OpCodes.Callvirt, callMethod, null);
         iLGenerator.Emit(OpCodes.Ret);


         return dynamicMethod.CreateDelegate(typeof(Action<T, object>)) as Action<T, object>;
     }
 }

public class ReflectionUtil
{
 /// <summary>
 /// 获取动态字段(属性)的实体属性值
 /// </summary>
 /// <typeparam name="T">实体类</typeparam>
 /// <param name="t">实体</param>
 /// <param name="property">属性名称</param>
 /// <returns></returns>
 public static Dictionary<object, object> GetPropertieValue<T>(T t,string property)
 {
     var ret = new Dictionary<object, object>();
     if (t == null) { return null; }
     PropertyInfo[] properties = t.GetType().GetProperties(BindingFlags.Instance | BindingFlags.Public);
     if (properties.Length <= 0) { return null; }

     foreach (PropertyInfo item in properties)
     {
         string name  = item.Name;
         object value = item.GetValue(t, null);
         if (item.Name == property)
             ret.Add(name, value);
     }
     return ret;
 }

}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值