惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News | PayPal Newsroom
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Engineering at Meta
Engineering at Meta
B
Blog RSS Feed
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
The Register - Security
The Register - Security
L
LangChain Blog
A
About on SuperTechFans
S
Schneier on Security
博客园 - 三生石上(FineUI控件)
Stack Overflow Blog
Stack Overflow Blog
The Hacker News
The Hacker News
AWS News Blog
AWS News Blog
博客园 - 司徒正美
Scott Helme
Scott Helme
K
Kaspersky official blog
Cyberwarzone
Cyberwarzone
T
Tenable Blog
腾讯CDC
Recorded Future
Recorded Future
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
G
GRAHAM CLULEY
Security Latest
Security Latest
S
Securelist
D
Darknet – Hacking Tools, Hacker News & Cyber Security
aimingoo的专栏
aimingoo的专栏
Google DeepMind News
Google DeepMind News
V
Vulnerabilities – Threatpost
雷峰网
雷峰网
T
The Exploit Database - CXSecurity.com
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
V
V2EX
T
The Blog of Author Tim Ferriss
D
Docker
S
Security Affairs
F
Full Disclosure
Know Your Adversary
Know Your Adversary
N
News and Events Feed by Topic
N
News and Events Feed by Topic
T
Tor Project blog
Hugging Face - Blog
Hugging Face - Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
Microsoft Security Blog
Microsoft Security Blog
Simon Willison's Weblog
Simon Willison's Weblog
Recent Announcements
Recent Announcements
博客园_首页
博客园 - 聂微东
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
S
Security @ Cisco Blogs

博客园 - sunjie

关于C#中async/await中的异常处理(上) 入门教程: JS认证和WebAPI ASP.NET Core 之 Identity 入门(二) 在Visual Studio 2017中使用Asp.Net Core构建Angular4应用程序 .Net Core+Angular Cli/Angular4开发环境搭建教程 简单易用的.NET免费开源RabbitMQ操作组件EasyNetQ解析 Razor - 模板引擎 / 代码生成 - RazorEngine 发布 Ionic iOS 企业级应用 AngularJS中的Provider们:Service和Factory等的区别 Angular $scope和$rootScope事件机制之$emit、$broadcast和$on Ionic开发实战 Entity Framework 5.0 Code First全面学习 6个强大的AngularJS扩展应用 使用npm安装一些包失败了的看过来(npm国内镜像介绍) 自己家里搭建NAS服务器有什么好方案? 自己动手制作CSharp编译器 使用Visual Studio Code搭建TypeScript开发环境 Office web app server2013详细的安装和部署 谈谈家庭装修中强电回路设计以及电线空开配套
一个简易的反射类库NMSReflector
sunjie · 2016-12-30 · via 博客园 - sunjie

Posted on 2016-12-30 08:52  sunjie  阅读(375)  评论()    收藏  举报

转自:http://blog.csdn.net/lanx_fly/article/details/53914338

背景简介

         以前看过一些代码,是简单的读取SqlReader然后赋值给Model,我不是不赞同这种做法,只是看到大篇幅的赋值操作真的有点浪费时间和精力,尤其是一些老项目居多。我看到的还好,多的也就60多个字段且不用其他ORM,如果涉及到变更的话,那么对维护人员来说可能不仅仅是眼力活甚至还是....体力活。另外就是表格的操作,因为鄙人之前也是写过类似的项目,列名对应着Model属性名,一个不差,隐隐觉得它们之间应该联系起来,所以想能不能尽可能简化它的操作?可能是自己做得项目太少,只能想到反射这种方法,但是反射的性能大家也都了解,大量的反射赋值耗时可以慢到你眨几下眼睛,但这对程序来说我觉得是一场灾难。因此结合反射发出的方法写了这个库,如果能给大家在项目上带来一些便利我也就知足了。

案例1:

  1. public class Student : INMSReflector {   
  2.     public string Name;  
  3.     public string Description { get; set; }          
  4.     public static string StaticField;   
  5.     public static string StaticProperty { get; set; }      
  6. }  

引用步骤:

  1. Step1 : 引用类库.   
  2. Step2 : using NMSReflector.  
  3. Step3 : 将你的类实现INMSReflector接口;(当然了,如果你嫌麻烦,可以改一下源码,在ModelOperator.cs中).  
  4. Step4 : 用Create方法创建缓存. (会扫描搜索入口程序集的所有类)  

由于类库中对object类型做了扩展,因此对象实例可以调用扩展方法。

1、EmitSet(string propertyName,object value)  为对象的字段或属性赋值

2、EmitGet(string propertyName) 获取对象某字段或者属性值 

用法:

  1. ModelOperator.Create();     
  2. Student t = new Student();     
  3. t.Name = "小明";     
  4. t.EmitSet("Name", "小明胸前的红领巾更加鲜艳了!");     
  5. Console.WriteLine(t.Name);     
  6. Console.WriteLine(t.EmitGet("Name"));     
  7. t.EmitSet("Description", "他爱着小刚");     
  8. Console.WriteLine(t.Description);     
  9. Console.WriteLine(t.EmitGet("Description"));     
  10. t.EmitSet("StaticFiled", "是他挨着小刚");     
  11. Console.WriteLine(Student.StaticField);     
  12. Console.WriteLine(t.EmitGet("StaticField"));     
  13. t.EmitSet("StaticProperty", "刚才打错了");     
  14. Console.WriteLine(Student.StaticProperty);     
  15. Console.WriteLine(t.EmitGet("StaticProperty"));    

结果:

案例2:

支持Column标签

  1. public class Student : INMSReflector   
  2.   
  3. {   
  4.   
  5.     public string Name;   
  6.   
  7.     [Column("Note")]   
  8.   
  9.     public string Description { get; set; }   
  10.   
  11.     public static string StaticField;   
  12.   
  13.     public static string StaticProperty { get; set; }   
  14.   
  15. }  

注意:

  1. 这里的标签是来自于System.ComponentModel.DataAnnotations.Schema;   
  2. 所以需要using System.ComponentModel.DataAnnotations.Schema;  

用法:

无论传标签设置的名字还是属性名,都可以赋值或者获取值。

  1. ModelOperator.Create();   
  2. Student t = new Student();   
  3. t.EmitSet("Note", "设置标签");   
  4. Console.WriteLine(t.Description);  
  5. Console.WriteLine(t.EmitGet("Note"));  

结果:

其他:

ModelOperator类提供了更多的操作函数。

  1. object的扩展方法有所不同,第一个参数需要把实例传进去  
  2. object Get<T>(T t, string propertyName)  
  3. void Set<T>(T t, string propertyName, object value)  
  4. Type GetType<T>(string propertyName)  
  5. Dictionary<string, Action<object, object>> GetSetCache<T>()  
  6. Dictionary<string, Func<object, object>> GetGetCache<T>()  
  7. Dictionary<string, Type> GetTypeCache<T>()  
  8. Dictionary<string, string> GetMapCache<T>()  

性能测试: