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

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

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

前言

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

直接项目引入类库使用

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

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

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

项目源代码

图片图片c3T28资讯网——每日最新资讯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/c3T28资讯网——每日最新资讯28at.com

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

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

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

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

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

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

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

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

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

标签:
  • 热门焦点
  • 帅气纯真少年!日本最帅初中生选美冠军出炉

    日本第一帅哥初一生选美大赛冠军现已正式出炉,冠军是来自千叶县的宗田悠良。日本一直热衷于各种选美大赛,从&ldquo;最美JK&rdquo;起到&ldquo;最美女星&r
  • 消息称迪士尼要拍真人版《魔发奇缘》:女主可能也找黑人演员

    8月5日消息,迪士尼确实有点忙,忙着将不少动画改成真人版,继《美人鱼》后,真人版《白雪公主》、《魔发奇缘》也在路上了。据外媒消息称,迪士尼将打造真人版
  • JavaScript学习 -AES加密算法

    引言在当今数字化时代,前端应用程序扮演着重要角色,用户的敏感数据经常在前端进行加密和解密操作。然而,这样的操作在网络传输和存储中可能会受到恶意攻击的威胁。为了确保数据
  • 最“俊美”淘宝卖家,靠直播和短视频圈粉,上架秒光,年销3000万

    来源 | 电商在线文|易琬玉编辑|斯问受访店铺:Ringdoll戒之人形图源:微博@御座的黄山、&ldquo;Ringdoll戒之人形&rdquo;淘宝店铺有关外貌的评价,黄山已经听累了。生于1985年的他,哪
  • 阿里大调整

    来源:产品刘有媒体报道称,近期淘宝天猫集团启动了近年来最大的人力制度改革,涉及员工绩效、层级体系等多个核心事项,目前已形成一个初步的&ldquo;征求意见版&rdquo;:1、取消P序列
  • 认真聊聊东方甄选:如何告别低垂的果实

    来源:山核桃作者:财经无忌爆火一年后,俞敏洪和他的东方甄选依旧是颇受外界关心的&ldquo;网红&rdquo;。7月5日至9日,为期5天的东方甄选&ldquo;甘肃行&rdquo;首次在自有App内直播,
  • 华为开发者大会2023日程公开:开设鸿蒙HarmonyOS 4体验区

    IT之家 7 月 31 日消息,华为今日公布了 HDC.Together 开发者大会 2023 的详细日程。整场大会将于 8 月 4 日-6 日之间举行,届时将发布最新一代鸿蒙 H
  • 2纳米决战2025

    集微网报道 从三强争霸到四雄逐鹿,2nm的厮杀声已然隐约传来。无论是老牌劲旅台积电、三星,还是誓言重回先进制程领先地位的英特尔,甚至初成立不久的新
  • iQOO Neo8 Pro抢先上架:首发天玑9200+ 安卓性能之王

    经过了一段时间的密集爆料,昨日iQOO官方如期对外宣布:将于5月23日推出全新的iQOO Neo8系列新品,官方称这是一款拥有旗舰级性能调校的作品。随着发布时
Top