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

· 浏览次数 : 24

小编点评

前言 今天大姚给大家分享一个实用且强大的C#工具类库:Z.Extension_methods。直接项目引入类库使用,在对应的NuGet包管理器中搜索Z.Extension_methods安装即可使用。支持.NET Standard 2.0和.NET Framework 4.0。 1. MD5哈希算法 public static partial class Extensions { /// /// A Stream extension method that converts the @this to a md5 hash. /// ///The @this to act on. ///@this as a string. 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(); } } 2. 解压GZip字节数组 public static partial class Extensions { /// /// A byte[] extension method that decompresses the byte array gzip to string. /// ///The @this to act on. ///The byte array gzip to string. 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.GetString(outStream.GetBuffer(), 0, totalBytes); } } } } 3. 将泛型数组转换为DataTable public static partial class Extensions { /// /// A T[] extension method that converts the @this to a DataTable. /// ///Generic type parameter. ///The @this to act on. ///@this as a DataTable. public static DataTable ToDataTable(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; } } 4. 支持在线搜索和演示在线地址:https://csharp-extension.com/en/online-example/ 搜索ToMD5Hash:使用.NET Fiddle在线演示:项目源码地址 更多项目实用功能和特性欢迎前往项目开源地址查看👀,别忘了给项目一个Star支持💖。 GitHub开源地址:https://github.com/zzzprojects/Z.ExtensionMethods

正文

前言

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

直接项目引入类库使用

在你的对应项目中NuGet包管理器中搜索:Z.ExtensionMethods安装即可使用。

支持.NET Standard 2.0和.NET Framework 4.0 。

项目源代码

部分扩展方法展示

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/

搜索ToMD5Hash:

使用.NET Fiddle在线演示:

项目源码地址

更多项目实用功能和特性欢迎前往项目开源地址查看👀,别忘了给项目一个Star支持💖。

优秀项目和框架精选

该项目已收录到C#/.NET/.NET Core优秀项目和框架精选中,关注优秀项目和框架精选能让你及时了解C#、.NET和.NET Core领域的最新动态和最佳实践,提高开发工作效率和质量。坑已挖,欢迎大家踊跃提交PR推荐或自荐(让优秀的项目和框架不被埋没🤞)。

DotNetGuide技术社区交流群

  • DotNetGuide技术社区是一个面向.NET开发者的开源技术社区,旨在为开发者们提供全面的C#/.NET/.NET Core相关学习资料、技术分享和咨询、项目框架推荐、求职和招聘资讯、以及解决问题的平台。
  • 在DotNetGuide技术社区中,开发者们可以分享自己的技术文章、项目经验、学习心得、遇到的疑难技术问题以及解决方案,并且还有机会结识志同道合的开发者。
  • 我们致力于构建一个积极向上、和谐友善的.NET技术交流平台。无论您是初学者还是有丰富经验的开发者,我们都希望能为您提供更多的价值和成长机会。

欢迎加入DotNetGuide技术社区微信交流群👪

与C#开源实用的工具类库,集成超过1000多种扩展方法相似的内容:

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

前言 今天大姚给大家分享一个C#开源(MIT License)、免费、实用且强大的工具类库,集成超过1000多种扩展方法增强 .NET Framework 和 .NET Core的使用效率:Z.ExtensionMethods。 直接项目引入类库使用 在你的对应项目中NuGet包管理器中搜索:Z.E

.NET周刊【6月第2期 2024-06-09】

国内文章 C#开源实用的工具类库,集成超过1000多种扩展方法 https://www.cnblogs.com/Can-daydayup/p/18230586 文章介绍了一个免费的C#工具类库Z.ExtensionMethods,可以通过NuGet包管理器轻松集成。该库支持.NET Standard

上周热点回顾(6.3-6.9)

热点随笔: · C#开源实用的工具类库,集成超过1000多种扩展方法 (追逐时光者)· RabbitMQ 进阶使用之延迟队列 → 订单在30分钟之内未支付则自动取消 (青石路)· .Net 中间件 - 新开源代码生成器 -ReZero (阿妮亚)· C#.Net筑基-String字符串超全总结 [深

C#的多线程UI窗体控件显示方案 - 开源研究系列文章

上次编写了《LUAgent服务器端工具》这个应用,然后里面需要新启动一个线程去对文件进行上传到FTP服务器,但是新线程里无法对应用主线程UI的内容进行更改,所以就需要在线程里设置主UI线程里控件信息的方法,于是就有了此博文。此文记录的是一种高级用法。 为了实际的使用,笔者将线程操作放在独立的类当中,

C#开源、功能强大、免费的Windows系统优化工具 - Optimizer

前言 今天给大家推荐一款由C#开源、功能强大、免费的Windows系统优化工具 - Optimizer。 工具介绍 Optimizer是一款功能强大的Windows系统优化工具,可帮助用户提高计算机性能、加强隐私和安全保护。该工具支持22种语言,同时提供了许多实用的功能,如关闭不必要的Windows

推荐一款C#开源的操作简单、免费的屏幕录制和GIF动画制作神器

前言 今天要给大家推荐一款由C#语言开发且开源的操作简单、免费的屏幕录制和GIF动画制作神器:ScreenToGif 。 工具介绍 ScreenToGif 是一款免费的开源屏幕录制和GIF 制作工具。它可以帮助用户捕捉计算机屏幕上的实时动画,并将其保存为高质量的 GIF 图像格式。该工具不仅适用于技

C# 实现Ping远程主机功能

C#实现Ping远程主机功能。 1、引用nuget包 Wesky.Net.OpenTools OpenTools是一个用于提高开发效率的开源工具库。该项目为个人开源项目,采用MIT开源协议,永不更改协议。开源项目地址: Gitee:https://gitee.com/dreamer_j/open-t

[转帖]CentOS7使用Chrony实现时间同步

学习安装部署 ceph 时 ,在添加 mon 时报错了,搜索原因后发现是 时间同步问题。于是学习一下时间同步工具。 一般CentOS6 使用的时间同步工具是ntp。现在还有不少开源软件文档建议安装的时间同步工具是ntp。个人感觉 chrony使用应该和ntp 差不多。本人使用 ntp较少,因为一直C

DotNetGuide专栏C#/.NET/.NET Core充电站(让你学习不迷路)

DotNetGuide简介 记录、收集和总结C#/.NET/.NET Core基础知识、学习路线、开发实战、编程技巧练习、学习视频、文章、书籍、项目框架、社区组织、开发必备工具、常见面试题、面试须知、简历模板、以及自己在学习和工作中的一些微薄见解。希望能和大家一起学习,共同进步。如果本知识库能为您提

DotNetGuide新增C#/.NET/.NET Core充电站(让你学习不迷路)

DotNetGuide简介 记录、收集和总结C#/.NET/.NET Core基础知识、学习路线、开发实战、学习视频、文章、书籍、项目框架、社区组织、开发必备工具、常见面试题、面试须知、简历模板、以及自己在学习和工作中的一些微薄见解。希望能和大家一起学习,共同进步👊【让现在的自己不再迷茫✨,如果本