星空网 > 软件开发 > ASP.net

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.ExtensionMethods 一个强大的开源扩展库

 

贴一个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(1, "Fizz");      @this.Rows.Add(2, "Buzz");      // Exemples      List<TestObject> entities = @this.ToEntities<TestObject>().ToList();      // Unit Test      Assert.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 底层更加了解!




原标题:Z.ExtensionMethods 一个强大的开源扩展库

关键词:

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。

负面反馈:https://www.goluckyvip.com/tag/5601.html
澳大利亚的标志性建筑物是什么:https://www.goluckyvip.com/tag/56010.html
德国标志性建筑:https://www.goluckyvip.com/tag/56011.html
世界卫生组织标志:https://www.goluckyvip.com/tag/56017.html
世界货币符号一览表:https://www.goluckyvip.com/tag/56018.html
差评撤掉:https://www.goluckyvip.com/tag/5602.html
月活用户超20亿!万亿市值巨头对中国商家进一步开闸放流 :https://www.kjdsnews.com/a/1836412.html
九寨沟周围必去的景点推荐:https://www.vstour.cn/a/363190.html
相关文章
我的浏览记录
最新相关资讯
海外公司注册 | 跨境电商服务平台 | 深圳旅行社 | 东南亚物流