今天有意的在博客园里面搜索了一下 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&lt;T&gt;</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()
{
// Type
string @this = "Ordinal"; // Examples
var 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()
{
// Type
string @thisValue = "Fizz";
string @thisNull = null; // Examples
bool value1 = @thisValue.IsNullOrEmpty(); // return false;
bool value2 = @thisNull.IsNullOrEmpty(); // return true; // Unit Test
Assert.IsFalse(value1);
Assert.IsTrue(value2);
}
}
}
using System.Collections.Generic;
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()
{
// Type
var @this = new DataTable(); // Variables
@this.Columns.AddRange("IntColumn", "StringColumn");
@this.Rows.Add(, "Fizz");
@this.Rows.Add(, "Buzz"); // Exemples
List<TestObject> entities = @this.ToEntities<TestObject>().ToList(); // Unit Test
Assert.AreEqual(, entities.Count);
Assert.AreEqual(, entities[].IntColumn);
Assert.AreEqual("Fizz", entities[].StringColumn);
Assert.AreEqual(, entities[].IntColumn);
Assert.AreEqual("Buzz", entities[].StringColumn);
} public class TestObject
{
public int IntColumn;
public int IntColumnNotExists = -;
public string StringColumnNotExists;
public string StringColumn { get; set; }
}
}
}

好了不多说了,大家如果要实现一些功能,可以参考开发文档,再看一下源代码,学习一下,也会有帮助,最受对.NET Framework 底层更加了解!

Z.ExtensionMethods 一个强大的开源扩展库的更多相关文章

  1. 发现一个强大的可视化第三方库pyecharts

    pyecharts 目前尚在不断的更新中,值得重点研究和学习的图表库

  2. underscore.js 一个强大的js函数库

    Underscore提供的100多个函数,主要涉及对Collection.Object.Array.Function的操作: Collections(集合) each, map, reduce, re ...

  3. Z.ExtensionMethods 扩展类库

    Z.ExtensionMethods 一个强大的开源扩展库 今天有意的在博客园里面搜索了一下 Z.ExtensionMethods 这个扩展类库,确发现只搜到跟这个真正相关的才两篇博文而已,我都点进去 ...

  4. Linux下经常使用的C/C++开源Socket库

    1.      Linux Socket Programming In C++ : http://tldp.org/LDP/LG/issue74/tougher.html 2.      ACE: h ...

  5. Linux下经常使用的C/C++开源Socket库【转】

    转自:https://www.cnblogs.com/gccbuaa/p/7015599.html 1.      Linux Socket Programming In C++ : http://t ...

  6. Element没更新了?Element没更新,基于El的扩展库更新

    think-vuele 基于Vue和ElementUI框架进行整合二次开发的一个框架.提供一些elementUI没有的或当时没有的控件.优化了或简化了便于2B软件开发的一些控件 demo:http:/ ...

  7. hellocharts-android开源图表库(效果非常好)

    泡在网上的日子 发表于 2014-11-07 12:28 第 33156 次阅读 chart 2 编辑推荐:稀土掘金,这是一个高质量的技术干货分享社区,web前端.Android.iOS.设计资源和产 ...

  8. Github上比较流行的PHP扩展库项目

    这里列出比较常用的PHP开源扩展库项目: swoole, C扩展实现的PHP异步并行网络通信框架,可以重新定义PHP.过去PHP只能做Web项目,现在有了Swoole.任意服务器端程序都可以用PHP来 ...

  9. GLEW扩展库【转】

    http://blog.sina.com.cn/s/blog_4aff14d50100ydsy.html 一.关于GLEW扩展库: GLEW是一个跨平台的C++扩展库,基于OpenGL图形接口.使用O ...

随机推荐

  1. 探索c#之尾递归编译器优化

    阅读目录: 递归运用 尾递归优化 编译器优化 递归运用 一个函数直接或间接的调用自身,这个函数即可叫做递归函数. 递归主要功能是把问题转换成较小规模的子问题,以子问题的解去逐渐逼近最终结果. 递归最重 ...

  2. [.net 面向对象程序设计深入](2)UML——在Visual Studio 2013/2015中设计UML用例图

    [.net 面向对象程序设计深入](2)UML——在Visual Studio 2013/2015中设计UML用例图  1.用例图简介 定义:用例图主要用来描述“用户.需求.系统功能单元”之间的关系. ...

  3. jieba中文分词的.NET版本:jieba.NET

    简介 平时经常用Python写些小程序.在做文本分析相关的事情时免不了进行中文分词,于是就遇到了用Python实现的结巴中文分词.jieba使用起来非常简单,同时分词的结果也令人印象深刻,有兴趣的可以 ...

  4. 移动APP服务端设计开发注意要点

    2014年,移动APP的热度丝毫没有减退,怎么为您的移动端app设计良好的服务器端接口(API)呢? 下面谈谈我个人的一些想法. 2014年,移动APP的热度丝毫没有减退,并没有像桌面软件被WEB网站 ...

  5. 《SQL必知必会》—— 读后总结

  6. CPU占用率呈正弦实现,及实时输出进程和线程的CPU占用率

    CPU占用率呈正弦实现,及实时输出进程和线程的CPU占用率 #include "stdafx.h" #include <windows.h> #include < ...

  7. 手写原生ajax

    关于手写原生ajax重要不重要,各位道友自己揣摩吧, 本着学习才能进步,分享大家共同受益,自己也在自己博客里写一下 function createXMLHTTPRequest() { //1.创建XM ...

  8. Ajax_05之跨域请求

    1.跨域请求: Cross Domain Request:跨域名的HTTP请求,浏览器从某个域名下的资源访问了另一域名下的另一资源(协议.域名或是端口号不同): ①浏览器允许跨域请求的情形:  < ...

  9. defered,promise回顾

    defered,promise回顾 http://www.ruanyifeng.com/blog/2011/08/a_detailed_explanation_of_jquery_deferred_o ...

  10. MVC实用架构设计(三)——EF-Code First(3):使用T4模板生成相似代码

    前言 经过前面EF的<第一篇>与<第二篇>,我们的数据层功能已经较为完善了,但有不少代码相似度较高,比如负责实体映射的 EntityConfiguration,负责仓储操作的I ...