Z.ExtensionMethods 一个强大的开源扩展库
? ? 今天有意的在博客園里面搜索了一下 Z.ExtensionMethods 這個擴展類庫,確發現只搜到跟這個真正相關的才兩篇博文而已,我都點進去看了一下,也都只是提到而已,沒有專門介紹,才引起我寫這篇文檔。
一.????? Z.ExtensionMethods 介紹
? ? Z.ExtensionMethods 是國外(zzzprojects 的公司,這家公司開發EntityFramework 擴展庫也很牛逼哦,不過要收費)開源的,且功能齊全,圍繞著.NET Framework 而開發擴展類庫,源代碼C#&VB.NET兩種語言。
Codeplex :http://zextensionmethods.codeplex.com/
GitHub:https://github.com/zzzprojects/Z.ExtensionMethods
在線文檔:http://www.zzzprojects.com/documentations/dotnet/extension-methods/
?
貼一個Z.Data 對DataTable 轉成 集合對象擴展,讓大家伙開開眼,看這些代碼熟悉不?
using System;using System.Collections.Generic;
using System.Data;
using System.Reflection;
public static partial class Extensions { ? ?/// <summary>/// ? ? Enumerates to entities in this collection. ? ?/// </summary>/// <typeparam name="T">Generic type parameter.</typeparam>/// <param name="this">The @this to act on.</param>/// <returns>@this as an IEnumerable<T></returns>public static IEnumerable<T> ToEntities<T>(this DataTable @this) where T : new(){Type type = typeof (T);PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance); ? ? ?
? ? ??var list = new List<T>(); ?
? ? ? ?foreach (DataRow dr in @this.Rows){ ? ? ? ? ? ?var entity = new T(); ? ? ? ? ?
? ? ? ? ? ??foreach (PropertyInfo property in properties){ ? ? ? ? ? ?
? ? ? ? ? ? ?? ?if (@this.Columns.Contains(property.Name)){Type valueType = property.PropertyType;property.SetValue(entity, dr[property.Name].To(valueType), null);}} ? ? ? ? ?
? ? ? ? ?foreach (FieldInfo field in fields) ? ? ?
? ?? ? ?{ ? ? ? ? ? ?
?? ? ? ? ?? ?if (@this.Columns.Contains(field.Name)){Type valueType = field.FieldType;field.SetValue(entity, dr[field.Name].To(valueType));}}list.Add(entity);} ? ? ? ?return list;} }
是不是感覺,之前我們自己寫過這樣的代碼,現在不用自己寫了,現成的拿來用就是,自己可以更加專注于更有意義的事情上,再來一段代碼。
public static partial class Extensions { ??/// <summary>/// ? ? A string extension method that queries if '@this' is null or is empty. ? ?/// </summary>/// <param name="this">The @this to act on.</param>/// <returns>true if '@this' is null or is empty, false if not.</returns>public static bool IsNullOrEmpty(this string @this){ ? ? ? ?
return string.IsNullOrEmpty(@this);} }
判斷字符串是否為空或Null,"字符串".IsNullOrEmpty() ?是不是更加能夠理解,感覺就像讀一句話一樣,
像這樣的DataTable轉對象集合以及判斷一個對象是否為空或者Null人性寫法,在Z.ExtensionMethods 擴展類庫里面到處能夠找到,大家有空可以打開它的源代碼學習一下。
一.??????Z.ExtensionMethods 使用
?1.?通過NuGet 程序包管理器,下載Z.ExtensionMethods Dll,右鍵-》你需要使用?Z.ExtensionMethods?類庫 項目-》管理NuGet程序包-》聯機-》右上角搜索“Z.ExtensionMethods” 下載安裝
?2. ?使用起來很簡單,下面是幾段單元測試代碼
using System;using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Z.Core.Test {[TestClass] ? ?
? public class System_String_ToEnum{[TestMethod] ? ? ?
? ? ? ?public void ToEnum(){ ? ? ? ? ? ?// Typestring @this = "Ordinal"; ? ? ? ? ? ?// Examplesvar value = @this.ToEnum<StringComparison>(); // return StringComparison.Ordinal; ? ? ? ? ? ?// Unit Test ? ? ?
? ? ? ? ? ?Assert.AreEqual(StringComparison.Ordinal, value);}} } using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Z.Core.Test {[TestClass] ? ?
public class System_String_IsNullOrEmpty{[TestMethod] ? ? ?
? ? ? ?public void IsNullOrEmpty(){ ? ? ? ? ? ?// Typestring @thisValue = "Fizz"; ? ? ? ?
? ? ? ?? ?string @thisNull = null; ? ? ? ? ? ?// Examplesbool value1 = @thisValue.IsNullOrEmpty(); // return false;bool value2 = @thisNull.IsNullOrEmpty(); // return true; ? ? ? ? ? ?// Unit Test ? ? ? ? ? ?Assert.IsFalse(value1);Assert.IsTrue(value2);}} }
using System.Data;
using System.Linq;
using Microsoft.VisualStudio.TestTools.UnitTesting;
namespace Z.Data.Test {[TestClass] ? ?
? public class System_Data_DataTable_ToEntities{[TestMethod] ? ?
? ?? ?public void ToEntities(){ ? ? ? ? ? ?// Typevar @this = new DataTable(); ? ? ? ? ? ?// Variables@this.Columns.AddRange("IntColumn", "StringColumn");@this.Rows.Add(1, "Fizz");@this.Rows.Add(2, "Buzz"); ? ? ? ? ? ?// ExemplesList<TestObject> entities = @this.ToEntities<TestObject>().ToList(); ? ? ? ? ? ?// Unit TestAssert.AreEqual(2, entities.Count);Assert.AreEqual(1, entities[0].IntColumn);Assert.AreEqual("Fizz", entities[0].StringColumn);Assert.AreEqual(2, entities[1].IntColumn);Assert.AreEqual("Buzz", entities[1].StringColumn);} ? ? ?
? ? ??public class TestObject{ ?
? ? ? ? ?public int IntColumn; ? ? ?
?? ? ?public int IntColumnNotExists = -1; ? ? ?
? ? ?public string StringColumnNotExists; ? ? ? ? ?
??public string StringColumn { get; set; }}} }
好了不多說了,大家如果要實現一些功能,可以參考開發文檔,再看一下源代碼,學習一下,也會有幫助,最受對.NET Framework 底層更加了解!
原文地址:http://www.cnblogs.com/davidzhou/p/5348460.html
.NET社區新聞,深度好文,微信中搜索dotNET跨平臺或掃描二維碼關注
總結
以上是生活随笔為你收集整理的Z.ExtensionMethods 一个强大的开源扩展库的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 熬夜并不值得程序员炫耀
- 下一篇: WEB API系列(一):WEB API