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

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

前言

今天大姚给大家分享一个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推荐或自荐(让优秀的项目和框架不被埋没🤞)。

相关文章
|
10天前
|
人工智能 开发者
您使用过哪些AI集成工具提升工作效率
【6月更文挑战第13天】您使用过哪些AI集成工具提升工作效率
|
3天前
|
Java API Spring
集成EasyPoi(一个基于POI的Excel导入导出工具)到Spring Boot项目中
集成EasyPoi(一个基于POI的Excel导入导出工具)到Spring Boot项目中
17 1
|
25天前
|
JSON API 定位技术
.NET集成DeveloperSharp实现http网络请求&与其它工具的比较
该内容介绍了一个支持.NET Core 2.0及以上和.NET Framework 4.0及以上的HTTP请求调用方法,主要讨论了POST和GET两种形式。POST请求较为常见,涉及调用地址、发送参数、HTTP请求头和编码格式设置。文中提供了一个使用DeveloperSharp库发送POST请求的C#代码示例,用于发送短信,其中`IU.HttpPost`方法用于执行POST请求。此外,还提到了`HttpPost`方法的参数和返回值说明。最后简要提及了GET请求,通常用于URL带有查询参数的情况,并给出一个简单的GET请求示例。
|
27天前
|
安全 数据管理 测试技术
网络安全与信息安全:防范漏洞、加强加密与提升安全意识深入探索自动化测试框架的设计原则与实践应用化测试解决方案。文章不仅涵盖了框架选择的标准,还详细阐述了如何根据项目需求定制测试流程,以及如何利用持续集成工具实现测试的自动触发和结果反馈。最后,文中还将讨论测试数据管理、测试用例优化及团队协作等关键问题,为读者提供全面的自动化测试框架设计与实施指南。
【5月更文挑战第27天】 在数字化时代,网络安全与信息安全已成为维护国家安全、企业利益和个人隐私的重要环节。本文旨在分享关于网络安全漏洞的识别与防范、加密技术的应用以及提升安全意识的重要性。通过对这些方面的深入探讨,我们希望能为读者提供一些实用的建议和策略,以应对日益严峻的网络安全挑战。 【5月更文挑战第27天】 在软件开发周期中,自动化测试作为保障软件质量的关键步骤,其重要性日益凸显。本文旨在剖析自动化测试框架设计的核心原则,并结合具体案例探讨其在实际应用中的执行策略。通过对比分析不同测试框架的优缺点,我们提出一套高效、可扩展且易于维护的自动
|
21天前
|
API Java 监控
SpringBoot基于OpenAPI3的接口文档管理快速集成和使用
本文主要简单介绍SpringCloud2023中进行接口文档管理,方便前后端开发和文档维护。文档管理工具基于开源的knife4j封装的openapi3。
54 3
|
3天前
|
消息中间件 Java Kafka
集成Kafka到Spring Boot项目中的步骤和配置
集成Kafka到Spring Boot项目中的步骤和配置
25 7
|
3天前
|
druid Java 关系型数据库
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
在Spring Boot中集成Druid实现多数据源有两种常用的方式:使用Spring Boot的自动配置和手动配置。
22 5
|
3天前
|
Java 数据库连接 mybatis
在Spring Boot应用中集成MyBatis与MyBatis-Plus
在Spring Boot应用中集成MyBatis与MyBatis-Plus
24 5
|
3天前
|
前端开发 JavaScript 安全
集成WebSocket在Spring Boot中可以用于实现实时的双向通信
集成WebSocket在Spring Boot中可以用于实现实时的双向通信
16 4
|
3天前
|
安全 Java Maven
在 Spring Boot 中实现邮件发送功能可以通过集成 Spring Boot 提供的邮件发送支持来完成
在 Spring Boot 中实现邮件发送功能可以通过集成 Spring Boot 提供的邮件发送支持来完成
11 2

热门文章

最新文章