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

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

时间:2024-06-06 09:33:00浏览次数:15  
标签:类库 string C# readBytes var new byte NET 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,byte,NET,1000
From: https://blog.csdn.net/qq_37237487/article/details/139375092

相关文章

  • Sql数据库利用linkserver和 CT[CHANGE_TRACKING]实现发布订阅
    源服务器初始化同步数据表SELECT*INTO【用于同步数据的表名】FROM( SELECTtop0 CT.SYS_CHANGE_VERSION, CT.SYS_CHANGE_OPERATION, CT.【同步数据表的主键ID】 FROMCHANGETABLE(CHANGES源数据表名,0)ASCT)t创建获取同步数据存储......
  • static 在c语言中的价值
    1.概要static在c语言中的价值。static还有一个在文件内部使用有效的限制。如果在函数的外部,被static修饰的变量或者函数,是不可以在文件外部访问的,也就是说不同的文件中,被static修饰的函数或者变量是可以重名的。static的这个特性,在c++中显得有些鸡肋,但在c中的价值却很大,几......
  • XML-RPC实现WebService简单PHP程序示例 及 Closure闭包中的bind与bindTo方法的区别
    一、XML-RPC实现WebService简单PHP程序示例    WebService就是为了异构系统的通信而产生的,它基本的思想就是使用基于XML的HTTP的远程调用提供一种标准的机制,而省去建立一种新协议的需求。目前进行WebService通信有两种协议标准,一种是XML-RPC,另外一种是SOAP。XML-RPC比较......
  • Linux 35.5 + JetPack v5.1.3@ ego-planner编译安装
    Linux35.5+JetPackv5.1.3@ego-planner编译安装1.源由2.编译&安装Step1:依赖库安装Step2:建立工程Step3:编译工程Step4:安装工程3.问题汇总3.1planner/plan_env-OpenCV3.2uav_simulator/local_sensing-CUDA优化4.总结1.源由Fast-PlannerFUELRACEReg......
  • stm32系列--DAC的应用
     #include"bsp_dac.h"#include"math.h"//#include"stm32f10x_tim.h"//#include"stm32f10x_dma.h"//#include"stm32f10x_dac.h"//正弦波单个周期的点数#definePOINT_NUM32#defineangle3.1415/64/*初始波形数据-----......
  • 主成分分析(PCA)介绍
    目录计算过程投影分量计算假设你有一家理发店,已经记录了过去一年中所有顾客的头发长度和发型偏好的数据。现在你想从这些数据中提取一些主要的信息,比如顾客最常选择的发型类型,以及不同发型之间的相关性等。这对于你未来开展有针对性的营销活动很有帮助。具体来说,我们可以将......
  • 高效AI出图工具Fooocus
    市面上有几大王牌,sd,comfyui,mj以及Fooocus安装https://github.com/lllyasviel/Fooocus下载后会有3个启动bat,根据自己选择,默认启动会联网下载模型模型下载模型路径为Fooocus\models\checkpoints,也可以用之前其他软件下载好的模型如果使用inpaint,会下载到Fooocus\models\inpai......
  • TCP通信——基于C语言连接
    设计两个程序分别作为服务器和客户端,互相进行连接服务器/*************************************************************************************************************************** filename: tcp_server.c* author :Dazz* date :2024/6/5* functio......
  • Sql数据库CT的开启
    1、数据库CT开启ALTERDATABASE[数据库名]SETCHANGE_TRACKING=ON(CHANGE_RETENTION=【Ct日志保留天数】DAYS,AUTO_CLEANUP=ON);关闭ALTERDATABASE[数据库名]SETCHANGE_TRACKING=OFF2、表CT开启ALTERTABLEdbo.[表名]ENABLECHANGE_TRACKINGWIT......
  • 如何创建一个线程池,为什么不推荐使用Executors去创建呢?
    我们在学线程的时候了解了几种创建线程的方式,比如继承Thread类,实现Runnable接口、Callable接口等,那对于线程池的使用,也需要去创建它,在这里我们提供2种构造线程池的方法:方法一:通过ThreadPoolExecutor构造函数来创建(首选)  这是JDK中最核心的线程池工具类,在JDK1.8中,它提供了丰......