温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

简易的反射类库NMSReflector该怎么理解

发布时间:2022-01-05 17:10:15 来源:亿速云 阅读:98 作者:柒染 栏目:大数据

简易的反射类库NMSReflector该怎么理解,针对这个问题,这篇文章详细介绍了相对应的分析和解答,希望可以帮助更多想解决这个问题的小伙伴找到更简单易行的方法。

背景简介

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

案例1:

public class Student : INMSReflector { 

public string Name;

public string Description { get; set; }        

public static string StaticField; 

public static string StaticProperty { get; set; }    

}

引用步骤:

  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) 获取对象某字段或者属性值 

用法:

ModelOperator.Create();   

Student t = new Student();   

//普通字段   

t.Name = "小明";   

t.EmitSet("Name", "小明胸前的红领巾更加鲜艳了!");   

Console.WriteLine(t.Name);   

Console.WriteLine(t.EmitGet("Name"));   

//普通属性   

t.EmitSet("Description", "他爱着小刚");   

Console.WriteLine(t.Description);   

Console.WriteLine(t.EmitGet("Description"));   

//静态字段   

t.EmitSet("StaticFiled", "是他挨着小刚");   

Console.WriteLine(Student.StaticField);   

Console.WriteLine(t.EmitGet("StaticField"));   

//静态属性   

t.EmitSet("StaticProperty", "刚才打错了");   

Console.WriteLine(Student.StaticProperty);   

Console.WriteLine(t.EmitGet("StaticProperty"));  

ModelOperator.Create();   
Student t = new Student();   
//普通字段   
t.Name = "小明";   
t.EmitSet("Name", "小明胸前的红领巾更加鲜艳了!");   
Console.WriteLine(t.Name);   
Console.WriteLine(t.EmitGet("Name"));   
//普通属性   
t.EmitSet("Description", "他爱着小刚");   
Console.WriteLine(t.Description);   
Console.WriteLine(t.EmitGet("Description"));   
//静态字段   
t.EmitSet("StaticFiled", "是他挨着小刚");   
Console.WriteLine(Student.StaticField);   
Console.WriteLine(t.EmitGet("StaticField"));   
//静态属性   
t.EmitSet("StaticProperty", "刚才打错了");   
Console.WriteLine(Student.StaticProperty);   
Console.WriteLine(t.EmitGet("StaticProperty"));

结果:

简易的反射类库NMSReflector该怎么理解

案例2:

支持Column标签

public class Student : INMSReflector 

public string Name; 

[Column("Note")] 

public string Description { get; set; } 

public static string StaticField; 

public static string StaticProperty { get; set; } 

}

public class Student : INMSReflector 

{ 

	public string Name; 

	[Column("Note")] 

	public string Description { get; set; } 

	public static string StaticField; 

	public static string StaticProperty { get; set; } 

}

注意:

这里的标签是来自于System.ComponentModel.DataAnnotations.Schema; 

所以需要using System.ComponentModel.DataAnnotations.Schema;

用法:

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

ModelOperator.Create(); 

Student t = new Student(); 

t.EmitSet("Note", "设置标签"); 

Console.WriteLine(t.Description);

Console.WriteLine(t.EmitGet("Note"));

其他:

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

与object的扩展方法有所不同,第一个参数需要把实例传进去

//获取实例t的某字段和属性的值

object Get<T>(T t, string propertyName)

//设置实例t的某字段和属性的值

void Set<T>(T t, string propertyName, object value)

//获取类型T的某字段和属性的类型

Type GetType<T>(string propertyName)

//获取类型T的设置方法缓存

Dictionary<string, Action<object, object>> GetSetCache<T>()

//获取类型T的获取方法缓存

Dictionary<string, Func<object, object>> GetGetCache<T>()

//获取类型T的属性字段类型缓存

Dictionary<string, Type> GetTypeCache<T>()

//获取类型T的标签与属性字段缓存

Dictionary<string, string> GetMapCache<T>()

性能测试:

简易的反射类库NMSReflector该怎么理解

关于简易的反射类库NMSReflector该怎么理解问题的解答就分享到这里了,希望以上内容可以对大家有一定的帮助,如果你还有很多疑惑没有解开,可以关注亿速云行业资讯频道了解更多相关知识。

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI