当前位置:首页 > 科技  > 软件

C#开源实用的工具类库,集成超过1000多种扩展方法

来源: 责编: 时间:2024-05-30 09:11:21 290观看
导读前言今天大姚给大家分享一个C#开源(MIT License)、免费、实用且强大的工具类库,集成超过1000多种扩展方法增强 .NET Framework 和 .NET Core的使用效率:Z.ExtensionMethods。直接项目引入类库使用在你的对应项目中NuGet包

前言

今天大姚给大家分享一个C#开源(MIT License)、免费、实用且强大的工具类库,集成超过1000多种扩展方法增强 .NET Framework 和 .NET Core的使用效率:Z.ExtensionMethods。LHb28资讯网——每日最新资讯28at.com

直接项目引入类库使用

在你的对应项目中NuGet包管理器中搜索:Z.ExtensionMethods安装即可使用。LHb28资讯网——每日最新资讯28at.com

支持.NET Standard 2.0和.NET Framework 4.0 。LHb28资讯网——每日最新资讯28at.com

图片图片LHb28资讯网——每日最新资讯28at.com

项目源代码

图片图片LHb28资讯网——每日最新资讯28at.com

部分扩展方法展示

MD5哈希算法

public static partial class Extensions{    /// <summary>    /// A Stream extension method that converts the @this to a md 5 hash.    /// </summary>    /// <param name="this">The @this to act on.</param>    /// <returns>@this as a string.</returns>    public static string ToMD5Hash(this Stream @this)    {        using (MD5 md5 = MD5.Create())        {            byte[] hashBytes = md5.ComputeHash(@this);            var sb = new StringBuilder();            foreach (byte bytes in hashBytes)            {                sb.Append(bytes.ToString("X2"));            }            return sb.ToString();        }    }}

解压GZip字节数组

public static partial class Extensions{    /// <summary>    /// A byte[] extension method that decompress the byte array gzip to string.    /// </summary>    /// <param name="this">The @this to act on.</param>    /// <returns>The byte array gzip to string.</returns>    public static string DecompressGZip(this byte[] @this)    {        const int bufferSize = 1024;        using (var memoryStream = new MemoryStream(@this))        {            using (var zipStream = new GZipStream(memoryStream, CompressionMode.Decompress))            {                // Memory stream for storing the decompressed bytes                using (var outStream = new MemoryStream())                {                    var buffer = new byte[bufferSize];                    int totalBytes = 0;                    int readBytes;                    while ((readBytes = zipStream.Read(buffer, 0, bufferSize)) > 0)                    {                        outStream.Write(buffer, 0, readBytes);                        totalBytes += readBytes;                    }                    return Encoding.Default.GetString(outStream.GetBuffer(), 0, totalBytes);                }            }        }    }    /// <summary>    /// A byte[] extension method that decompress the byte array gzip to string.    /// </summary>    /// <param name="this">The @this to act on.</param>    /// <param name="encoding">The encoding.</param>    /// <returns>The byte array gzip to string.</returns>    public static string DecompressGZip(this byte[] @this, Encoding encoding)    {        const int bufferSize = 1024;        using (var memoryStream = new MemoryStream(@this))        {            using (var zipStream = new GZipStream(memoryStream, CompressionMode.Decompress))            {                // Memory stream for storing the decompressed bytes                using (var outStream = new MemoryStream())                {                    var buffer = new byte[bufferSize];                    int totalBytes = 0;                    int readBytes;                    while ((readBytes = zipStream.Read(buffer, 0, bufferSize)) > 0)                    {                        outStream.Write(buffer, 0, readBytes);                        totalBytes += readBytes;                    }                    return encoding.GetString(outStream.GetBuffer(), 0, totalBytes);                }            }        }    }}

将泛型数组转换为DataTable

public static partial class Extensions{    /// <summary>    /// A T[] extension method that converts the @this to a data table.    /// </summary>    /// <typeparam name="T">Generic type parameter.</typeparam>    /// <param name="this">The @this to act on.</param>    /// <returns>@this as a DataTable.</returns>    public static DataTable ToDataTable<T>(this T[] @this)    {        Type type = typeof (T);        PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);        FieldInfo[] fields = type.GetFields(BindingFlags.Public | BindingFlags.Instance);        var dt = new DataTable();        foreach (PropertyInfo property in properties)        {            dt.Columns.Add(property.Name, property.PropertyType);        }        foreach (FieldInfo field in fields)        {            dt.Columns.Add(field.Name, field.FieldType);        }        foreach (T item in @this)        {            DataRow dr = dt.NewRow();            foreach (PropertyInfo property in properties)            {                dr[property.Name] = property.GetValue(item, null);            }            foreach (FieldInfo field in fields)            {                dr[field.Name] = field.GetValue(item);            }            dt.Rows.Add(dr);        }        return dt;    }}

支持在线搜索和演示

在线地址:https://csharp-extension.com/en/online-example/LHb28资讯网——每日最新资讯28at.com

图片图片LHb28资讯网——每日最新资讯28at.com

搜索ToMD5Hash:LHb28资讯网——每日最新资讯28at.com

图片图片LHb28资讯网——每日最新资讯28at.com

使用.NET Fiddle在线演示:LHb28资讯网——每日最新资讯28at.com

图片 图片 LHb28资讯网——每日最新资讯28at.com

本文链接:http://www.28at.com/showinfo-26-91685-0.htmlC#开源实用的工具类库,集成超过1000多种扩展方法

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。邮件:2376512515@qq.com

上一篇: 面试官:说说Netty核心组件?

下一篇: 尤雨溪:这个前端经典轮子值得去造!

标签:
  • 热门焦点
  • MIX Fold3包装盒泄露 新机本月登场

    小米的全新折叠屏旗舰MIX Fold3将于本月发布,近日该机的真机包装盒在网上泄露。从图上来看,新的MIX Fold3包装盒在外观设计方面延续了之前的方案,变化不大,这也是目前小米旗舰
  • 小米官宣:2023年上半年出货量中国第一!

    今日早间,小米电视官方微博带来消息,称2023年小米电视上半年出货量达到了中国第一,同时还表示小米电视的巨屏风暴即将开始。“公布一个好消息2023年#小米电视上半年出货量中国
  • 直屏旗舰来了 iQOO 12和K70 Pro同台竞技

    旗舰机基本上使用的都是双曲面屏幕,这就让很多喜欢直屏的爱好者在苦等一款直屏旗舰,这次,你们等到了。据博主数码闲聊站带来的最新爆料称,Redmi下代旗舰K70 Pro和iQOO 12两款手
  • 中兴AX5400Pro+上手体验:再升级 双2.5G网口+USB 3.0这次全都有

    2021年11月的时候,中兴先后发布了两款路由器产品,中兴AX5400和中兴AX5400 Pro,从产品命名上就不难看出这是隶属于同一系列的,但在外观设计上这两款产品可以说是完全没一点关系
  • 跑分安卓第一!Redmi K60至尊版8月发布!卢伟冰:目标年度性能之王

    8月5日消息,Redmi K60至尊版将于8月发布,在此前举行的战略发布会上,官方该机将搭载搭载天玑9200+处理器,安兔兔V10跑分超177万分,是目前安卓阵营最高的分数
  • 多线程开发带来的问题与解决方法

    使用多线程主要会带来以下几个问题:(一)线程安全问题  线程安全问题指的是在某一线程从开始访问到结束访问某一数据期间,该数据被其他的线程所修改,那么对于当前线程而言,该线程
  • 大厂卷向扁平化

    来源:新熵作者丨南枝 编辑丨月见大厂职级不香了。俗话说,兵无常势,水无常形,互联网企业调整职级体系并不稀奇。7月13日,淘宝天猫集团启动了近年来最大的人力制度改革,目前已形成一
  • 网传小米汽车开始筛选交付中心 建筑面积不低于3000平方米

    7月7日消息,近日有微博网友@长三角行健者爆料称,据经销商集团反馈,小米汽车目前已经开始了交付中心的筛选工作,要求候选场地至少有120个车位,建筑不能低
  • iQOO Neo8系列今日官宣:首发天玑9200+ 全球安卓最强芯!

    在昨日举行的的联发科新一代旗舰芯片天玑9200+的发布会上,iQOO官方也正式宣布,全新的iQOO Neo8系列新品将全球首发搭载这款当前性能最强大的移动平台
Top