首页 > 编程语言 >C#开源实用的工具类库,集成超过1000多种扩展方法

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

时间:2024-06-04 13:46:14浏览次数:7  
标签:类库 string C# readBytes var new NET byte 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在线演示:

项目源码地址

更多项目实用功能和特性欢迎前往项目开源地址查看

标签:类库,string,C#,readBytes,var,new,NET,byte,1000
From: https://www.cnblogs.com/Can-daydayup/p/18230586

相关文章

  • CentOS-7.9 安装rabbitmq3.9.11 ,erlang-23.3.4.11
    下载所需rpm包wget https://github.com/rabbitmq/erlang-rpm/releases/download/v23.3.4.11/erlang-23.3.4.11-1.el7.x86_64.rpmwget https://github.com/rabbitmq/rabbitmq-server/releases/download/v3.9.11/rabbitmq-server-3.9.11-1.el7.noarch.rpm安装Erlangsu......
  • 探索sqlmap在WebSocket安全测试中的应用
    探索sqlmap在WebSocket安全测试中的应用WebSocket与HTTP的区别WebSocket,对于初次接触的人来说,往往会引发一个疑问:既然我们已经有了广泛使用的HTTP协议,为何还需要引入另一种协议?WebSocket又能为我们带来哪些实质性的好处呢?这背后的答案在于HTTP协议的一个关键限制——通信的发起......
  • Buuctf-Web(37-42)
    37[网鼎杯2020朱雀组]phpweb参考[网鼎杯2020朱雀组]phpweb1-CSDN博客[buuctf-网鼎杯2020朱雀组]phpweb(小宇特详解)_buuctf[网鼎杯2020朱雀组]phpweb-CSDN博客打开是这个界面,大概5秒刷新一次先来一遍扫目录,看源码,抓包一扫就409,应该做了限制,源码没发现什么,抓包看看......
  • Buuctf-Web(1-6)
    1[极客大挑战2019]EasySQL根据题目,用单引号检测是否存在SQL注入漏洞分析报错信息:看'单引号后面跟着的字符,是什么字符,它的闭合字符就是什么,若是没有,就为数字型。在两个位置用单引号测试,发现闭合符号为单引号,注入点在password万能密码法常规法注入流程数据库->表->字段-......
  • LeetCode 2903.找出满足差值条件的下标I
    1.题目要求如图所示:由题意可知我们如果要满足差值条件,我们可以使用迭代法,让下标i从零开始,我们再设一个变量j,然后让j等于下标加上indexDifference,再用for循环遍历j,再采用abs(nums[i]-nums[j])是否大于等于valueDifference,如果大于则break;以上就是代码算法思路接下来......
  • Leetcode 151.反转字符串中的单词
    ​此题是非常经典的字符串的颠倒问题,但这个更复杂一些,但也不其本质,我此次写的方式是用双指针问题完成的,虽然算不上什么好方法,但如果各位看官觉得满意的话,请各位给我个点个免费的赞吧,谢谢了_1.题目要求如图所示:2.接下来是做题的步骤:我们先把字符串的颠倒函数写好,如图......
  • C++发票查验真假的接口厂家有哪些?数电票、医疗票据查验
    现如今,随着数字化应用的不断普及,财务工作也在不断的由人工向数字化转型。企业财务进行数字化转型,能够推动财务管理职能的转型升级,通过运用云计算、大数据等技术,重构财务组合和业务流程,实现业财融合,提升会计信息质量、工作效率、合规程度及价值创造能力。翔云人工智能开放平......
  • 适合小白学习的项目1901java体育馆管理系统Myeclipse开发mysql数据库web结构java编程
    一、源码特点java体育馆管理系统是一套完善的web设计系统,对理解JSPjava编程开发语言有帮助采用了java设计,系统具有完整的源代码和数据库,系统采用web模式,系统主要采用B/S模式开发。开发环境为TOMCAT7.0,Myeclipse8.5开发,数据库为Mysql,使用java语言开发。java体育馆管理系......
  • atcoder ABC 356-B题详解
    atcoderABC356-B题详解ProblemStatementTakahashiishealth-consciousandconcernedaboutwhetherheisgettingenoughofMtypesofnutrientsfromhisdiet.Forthei-thnutrient,hisgoalistotakeatleastAi​unitsperday.Today,heateNfoods......
  • PySpark JDBC 读写 MySQL 数据库保姆级指南
    目录1.环境准备1.1安装PySpark1.2MySQLJDBC驱动2.PySparkJDBC连接配置2.1JDBCURL......