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

}
## 比较全面的c#帮助 日常工作总结,加上网上收集,各式各样的几乎都能找到,所有功能性代码都是独立的之间没有联系,可以单独引用至项目,分享出来,方便大家,几乎都有注释,喜欢的请点赞,不断完善收集中... ## 样板图片操作 ![WEFE@M%}SN4_K$6H0D{6IYJ.png](http://upload-images.jianshu.io/upload_images/6855212-34f0ee0339e3cb49.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) ## 操作文档 里面包含一下操作文档,这个是用Sandcastle工具生成的。方法:四种Sandcastle方法生成c#.net帮助帮助文档,地址:http://www.cnblogs.com/anyushengcms/p/7682501.html ![H819EQUYFVA~WXK6YAQ1%6Q.png](http://upload-images.jianshu.io/upload_images/6855212-6cf5a7a2a4a75c89.png?imageMogr2/auto-orient/strip|imageView2/2/w/1240) ## 附上一些常见的帮助栏目 1. cookie操作 --------- CookieHelper.cs 2. session操作 ------- SessionHelper.cs 3. cache操作 4. ftp操作 5. http操作 ------------ HttpHelper.cs 6. json操作 ------------ JsonHelper.cs 7. xml操作 ------------- XmlHelper.cs 8. Excel操作 9. Sql操作 ------------- SqlHelper.cs 10. 型转换 ------------ Converter.cs 11. 加密解密 ------------ EncryptHelper.cs 12. 邮件发送 ------------ MailHelper.cs 13. 二维码 14. 汉字转拼音 15. 计划任务 ------------ IntervalTask.cs 16. 信息配置 ------------ Setting.cs 17. 上传下载配置文件操作 18. 视频转换 19. 图片操作 20. 验证码生成 21. String拓展 ---------- StringExtension.cs 22. 正则表达式 --------- RegexHelper.cs 23. 分页操作 24. UBB编码 25. Url重写 26. Object拓展 --------- ObjectExtension.cs 27. Stream的拓展 ------ StreamExtension.cs 28. CSV文件转换 29. Chart图形 30. H5-微信 31. PDF 32. 分词辅助 33. 序列化 34. 异步线程 35. 弹出消息 36. 文件操作 37. 日历 38. 日志 39. 时间操作 40. 时间戳 41. 条形码 42. 正则表达式 43. 汉字转拼音 44. 网站安全 45. 网络 46. 视频转换 47. 计划任务 48. 配置文件操作 49. 阿里云 50. 随机数 51. 页面辅助 52. 验证码 53. Mime 54. Net 55. NPOI 56. obj 57. Path 58. Properties 59. ResourceManager 60. URL的操作 61. VerifyCode 62. 处理多媒体的公共 63. 各种验证帮助 64. 分页 65. 计划任务 66. 配置文件操作 67. 分词辅助 68. IP辅助 69. Html操作
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值