一个简易的反射类库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; } ? ?
}
引用步驟:
Step1?:?引用類庫.???
Step2?:?using?NMSReflector.??
Step3?:?將你的類實現INMSReflector接口;(當然了,如果你嫌麻煩,可以改一下源碼,在ModelOperator.cs中).??
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"));結果:
案例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>()
性能測試:
項目地址:https://github.com/NMSLanX/NMSReflector
原文地址:http://blog.csdn.net/lanx_fly/article/details/53914338
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎總結
以上是生活随笔為你收集整理的一个简易的反射类库NMSReflector的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Visual Studio Code 1
- 下一篇: 管理软件公司与互联网公司的区别