应无所住,而生其心
排名
1
文章
860
粉丝
112
评论
163
net core webapi post传递参数
庸人 : 确实坑哈,我也是下班好了好几次,发现后台传递对象是可以的,但...
百度编辑器自定义模板
庸人 : 我建议换个编辑器,因为现在百度富文本已经停止维护了,用tinymec...
ICP备案 :渝ICP备18016597号-1
网站信息:2018-2025TNBLOG.NET
技术交流:群号656732739
联系我们:contact@tnblog.net
公网安备:50010702506256
欢迎加群交流技术

.net core 反射特性使用,反射通过特性匹配需要的字段赋值

1095人阅读 2021/11/7 11:49 总访问:5198818 评论:0 收藏:0 手机
分类: .NET Core

有这样一个需求,数据库查询出来是这样的数据

要根据tag来获取数据给字段赋值,这种情况一般是使用行专列处理后在前台显示,但是我们这里需求有点特殊,也不太好使用行专列来处理。如果直接写死代码的话就是这个样子

  1. var tagCountListAll = BaseDal.Db.Queryable<StuElevateAssignment>().
  2. Where(a => classIdlist.Contains(a.ClassId) && a.DAStatus != 1 && a.ASSType == _params.ASSType && a.CreateTime >= _params.dateTime && a.CreateTime < nextDay)
  3. .GroupBy(a => new { a.Tag, a.ClassId }).Select(a => new
  4. {
  5. ClassId = a.ClassId,
  6. Tag = a.Tag,
  7. Count = SqlFunc.AggregateCount(a.Tag),
  8. }).ToList();
  9. foreach (VClassTeam item in vClassTeams)
  10. {
  11. var tagCountList = tagCountListAll.Where(a => a.ClassId == item.ID).ToList();
  12. ClassStatisticsShowStyleDto classStatisticsDto = new ClassStatisticsShowStyleDto();
  13. classStatisticsDto.ClassId = item.ID;
  14. classStatisticsDto.ClassName = item.ClassName;
  15. if (tagCountList.First(a => a.Tag == "plank") == null)
  16. {
  17. classStatisticsDto.PlankCount = "/";
  18. }
  19. else
  20. {
  21. classStatisticsDto.PlankCount = tagCountList.First(a => a.Tag == "plank").Count + "";
  22. }
  23. if (tagCountList.First(a => a.Tag == "仰卧起坐") == null)
  24. {
  25. classStatisticsDto.SitUpsCount = "/";
  26. }
  27. else
  28. {
  29. classStatisticsDto.SitUpsCount = tagCountList.First(a => a.Tag == "仰卧起坐").Count + "";
  30. }
  31. if (tagCountList.First(a => a.Tag == "俯卧撑") == null)
  32. {
  33. classStatisticsDto.PushUpCount = "/";
  34. }
  35. else
  36. {
  37. classStatisticsDto.PushUpCount = tagCountList.First(a => a.Tag == "俯卧撑").Count + "";
  38. }
  39. if (tagCountList.First(a => a.Tag == "健身房锻炼") == null)
  40. {
  41. classStatisticsDto.GymExerciseCount = "/";
  42. }
  43. else
  44. {
  45. classStatisticsDto.GymExerciseCount = tagCountList.First(a => a.Tag == "健身房锻炼").Count + "";
  46. }
  47. if (tagCountList.First(a => a.Tag == "其它") == null)
  48. {
  49. classStatisticsDto.OtherCount = "/";
  50. }
  51. else
  52. {
  53. classStatisticsDto.OtherCount = tagCountList.First(a => a.Tag == "其它").Count + "";
  54. }
  55. if (tagCountList.First(a => a.Tag == "球类运动") == null)
  56. {
  57. classStatisticsDto.BallCount = "/";
  58. }
  59. else
  60. {
  61. classStatisticsDto.BallCount = tagCountList.First(a => a.Tag == "球类运动").Count + "";
  62. }
  63. if (tagCountList.First(a => a.Tag == "田径类") == null)
  64. {
  65. classStatisticsDto.TrackCount = "/";
  66. }
  67. else
  68. {
  69. classStatisticsDto.TrackCount = tagCountList.First(a => a.Tag == "田径类").Count + "";
  70. }
  71. if (tagCountList.First(a => a.Tag == "跳绳") == null)
  72. {
  73. classStatisticsDto.RopeSkippingCount = "/";
  74. }
  75. else
  76. {
  77. classStatisticsDto.RopeSkippingCount = tagCountList.First(a => a.Tag == "跳绳").Count + "";
  78. }
  79. classStatisticsDtoList.Add(classStatisticsDto);
  80. }

这种代码写起来就太不灵活了,太硬编码了,而且我们这里类型还不止这些,这样写的话代码会写得超级长,很难受,如果这里的类型有做数据字典的情况还会,用这里的tag和数据字典的字段匹配实体的属性名,用来反射给实体的属性赋值就行了,但是这里设计数据库的太坑了,数据字典也没有,所以我们只能想其他方法了,这里就可以考虑用特性来做一个映射,然后在使用反射。

配置特性,其实就是让中文的tag和字段做一个映射,有了这个映射就可以使用反射来赋值了

  1. public class ClassStatisticsShowStyleDto
  2. {
  3. public string ClassName { get; set; }
  4. public string ClassId { get; set; }
  5. [DisplayName("plank")]
  6. public string PlankCount { get; set; }
  7. [DisplayName("仰卧起坐")]
  8. public string SitUpsCount { get; set; }
  9. [DisplayName("俯卧撑")]
  10. public string PushUpCount { get; set; }
  11. [DisplayName("跳绳")]
  12. public string RopeSkippingCount { get; set; }
  13. [DisplayName("田径类")]
  14. public string TrackCount { get; set; }
  15. [DisplayName("球类运动")]
  16. public string BallCount { get; set; }
  17. [DisplayName("健身房锻炼")]
  18. public string GymExerciseCount { get; set; }
  19. [DisplayName("其它")]
  20. public string OtherCount { get; set; }
  21. }

有了这个映射就可以使用反射来赋值了:

  1. var tagCountListAll = BaseDal.Db.Queryable<StuElevateAssignment>().
  2. Where(a => classIdlist.Contains(a.ClassId) && a.DAStatus != 1 && a.ASSType == _params.ASSType && a.CreateTime >= _params.dateTime && a.CreateTime < nextDay)
  3. .GroupBy(a => new { a.Tag, a.ClassId }).Select(a => new
  4. {
  5. ClassId = a.ClassId,
  6. Tag = a.Tag,
  7. Count = SqlFunc.AggregateCount(a.Tag),
  8. }).ToList();
  9. foreach (VClassTeam item in vClassTeams)
  10. {
  11. var tagCountList = tagCountListAll.Where(a => a.ClassId == item.ID).ToList();
  12. ClassStatisticsShowStyleDto classStatisticsDto = new ClassStatisticsShowStyleDto();
  13. classStatisticsDto.ClassId = item.ID;
  14. classStatisticsDto.ClassName = item.ClassName;
  15. // 反射获取所有属性。用反射来代替下面的写死数据查询
  16. PropertyInfo[] properties = classStatisticsDto.GetType().GetProperties();
  17. foreach (PropertyInfo propertyInfo in properties)
  18. {
  19. var attrs = propertyInfo.GetCustomAttributes(typeof(DisplayNameAttribute), true);
  20. if (attrs != null)
  21. {
  22. if (attrs.Length > 0)
  23. {
  24. var displayName = ((DisplayNameAttribute)attrs[0]).DisplayName;
  25. // 用特性来找到需要的数据在赋值
  26. if (!string.IsNullOrWhiteSpace(displayName))
  27. {
  28. if (tagCountList.FirstOrDefault(a => a.Tag == displayName) == null)
  29. {
  30. //classStatisticsDto.PlankCount = "/";
  31. propertyInfo.SetValue(classStatisticsDto, "/");
  32. }
  33. else
  34. {
  35. //classStatisticsDto.PlankCount = tagCountList.First(a => a.Tag == displayName).Count + "";
  36. propertyInfo.SetValue(classStatisticsDto, tagCountList.First(a => a.Tag == displayName).Count + "");
  37. }
  38. }
  39. }
  40. }
  41. }
  42. }

欢迎加群讨论技术,1群:677373950(满了,可以加,但通过不了),2群:656732739。有需要软件开发,或者学习软件技术的朋友可以和我联系~(Q:815170684)

评价

net core 使用 EF Code First

下面这些内容很老了看这篇:https://www.tnblog.net/aojiancc2/article/details/5365 项目使用多层,把数据库访问...

.net mvc分部页.net core分部页

.net分部页的三种方式第一种:@Html.Partial(&quot;_分部页&quot;)第二种:@{ Html.RenderPartial(&quot;分部页&quot;);}...

StackExchange.Redis操作redis(net core支持)

官方git开源地址https://github.com/StackExchange/StackExchange.Redis官方文档在docs里边都是官方的文档通过nuget命令下...

.net core 使用session

tip:net core 2.2后可以直接启用session了,不用在自己添加一次session依赖,本身就添加了使用nuget添加引用Microsoft.AspN...

通俗易懂什么是.net?什么是.net Framework?什么是.net core?

朋友圈@蓝羽 看到一篇文章写的太详细太通俗了,搬过来细细看完,保证你对.NET有个新的认识理解原文地址:https://www.cnblo...

asp.net core2.0 依赖注入 AddTransient与AddScoped的区别

asp.net core主要提供了三种依赖注入的方式其中AddTransient与AddSingleton比较好区别AddTransient瞬时模式:每次都获取一...

.net core 使用 Kestrel

Kestrel介绍 Kestrel是一个基于libuv的跨平台web服务器 在.net core项目中就可以不一定要发布在iis下面了Kestrel体验可以使...

net core中使用cookie

net core中可以使用传统的cookie也可以使用加密的cookieNET CORE中使用传统cookie设置:HttpContext.Response.Cookies.Appe...

net core项目结构简单分析

一:wwwrootwwwroot用于存放网站的静态资源,例如css,js,图片与相关的前端插件等lib主要是第三方的插件,例如微软默认引用...

net core使用EF之DB First

一.新建一个.net core的MVC项目新建好项目后,不能像以前一样直接在新建项中添加ef了,需要用命令在添加ef的依赖二.使用Nug...

.net core使用requestresponse下载文件下载excel等

使用request获取内容net core中request没有直接的索引方法,需要点里边的Query,或者formstringbase64=Request.Form[&quot;f...

iframe自适应高度与配合net core使用

去掉iframe边框frameborder=&quot;0&quot;去掉滚动条scrolling=&quot;no&quot;iframe 自适应高度如果内容是固定的,那么就...

net core启动报错Unable to configure HTTPS endpoint. No server certificate was specified

这是因为net core2.1默认使用的https,如果使用Kestrel web服务器的话没有安装证书就会报这个错其实仔细看他的错误提示,其...

net core中使用url编码与解码操作

net core中暂时还没有以前asp.net与mvc中的server对象。获取url的编码与解码操作不能使用以前的server对象来获取。使用的是...

下载net core

官方下载地址:https://dotnet.microsoft.com/download 进来之后就可以看到最新的下载版本可以直接点击下载,也可以下载其...

net core使用依赖注入来装载EF的上下文对象

妹子情人节快乐~.net core中用了不少的依赖注入,官方文档中也推荐使用。这样使用依赖注入来管理ef对象,还是比较科学,比如...