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

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

时间:2024-06-06 12:24:03浏览次数:9  
标签:类库 string C# readBytes var new NET byte 1000

https://www.cnblogs.com/Can-daydayup/p/18230586

今天大姚给大家分享一个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/chinasoft/p/18234892

相关文章

  • Azure 函数(Node.js):如何从代码中读取上传到 Azure 证书刀片的公钥(.cer)?
    我在Node.js上运行了AzureFunctions。(另外,请注意,通过应用程序服务计划运行的底层操作系统是Windows)让我先绕道而行。一直以来,我都是从Node.js代码访问AzureKeyVault,因此我知道如何从Node.js代码访问KeyVault,这样看起来就像这样:co......
  • C# .NET 6 使用WorkFlow Core 创建工作审批流
    1,背景工作流思想在上世纪60年代就有人提出过;70年代就有人开始尝试,但是由于当时许多的限制,工作流一直没有成功的被实现;80年代才出现第一批成功的工作流系统;90年代工作流技术走向了第一个发展高峰期;90年代后至今工作流出现了很多版本,但是主旨还是不变的,为了使我们的工作变得更......
  • Python - Django - MySQL #need to add distinct() after select_related().distinct(
    所以这是ads/views.py还有ads/models.py、ads/forms、ads/urls.py和其他文件,但评分器抱怨的是这个views.py...检索到3806个HTML字符测试已完成:在页面顶部发现菜单栏搜索"HHGTTG_421717639962"时发现多个广告。您可能需要在views.py中的select_related().di......
  • 全局 type 类型的寻找 typescript 类型 - fabric.Canvas
    全局type类型的寻找typescript类型-fabric.CanvasdeclaretypeExtCanvas=fabric.Canvas&{isDragging:boolean;lastPosX:number;lastPosY:number;};这个代码没有定义fabric.Canvas,然后看看提示说在namespace定义了。这个是子项目,没有type定义,上父项......
  • 微星主板首秀CAMM2、Mini_CUDIMM两大革命性内存!下代普及四大徒手DIY设计
    日前,微星全球第一家宣布了配置CAMM2内存的主板,而在台北电脑展上,我们终于见识到了它,暂定名“Z790PROJECTZEROPLUSCAMM2”。CAMM2已经在2023年底正式成为内存行业标准形态,从传统DIMM的立式变成了“躺卧式”,高度空间占用大大压缩,容量可以做得更大目前已经达256GB,而且单条就能组......
  • abc355f 题解
    abc355f直接贺lct维护mst的代码。思路观察到\(w_i\le10\),考虑分开建\(10\)个图表示边权小于等于\(i\)的边组成的图。连并查集,记录当前图连了\(siz_i\)条边。可以发现第\(i-1\)个图是第\(i\)个图的子图。所以差分\(siz_i-siz_{i-1}\)可以得到原图的最小生成......
  • CF1575E 题解
    CF1575E思路点分治,记录当前子树到分治中心的权值和和换车次数。将新子树的答案合并时分类讨论分治中心到子树祖先\(u\tov\)的颜色。树状数组维护前缀和。复杂度\(O(n\log^2n)\)。codeintn,k,a[maxn],ans;inthead[maxn],tot;structnd{ intnxt,to,fl;}e[maxn<<1];......
  • abc355e 题解
    abc355e思路WC2024T3中知道一个技巧:如果知道区间\([l,r]\)的和就连边\(l\tor+1\),那么想推出\([L,R]\)的区间和就要求\(L\)和\(R+1\)联通。按题意把符合要求的边连上,设边权为\(1\)跑bfs,求出\(L\)到\(R+1\)的最短路并记录路径上的点,就可以得到要询问的区间。......
  • CF1007B 题解
    CF1007B思路显然题目要求计数\(u\midA,v\midB,w\midC\)。\(O(n\sqrtn)\)预处理出每个数的所有因数,记为集合\(p_i\)。容斥,记集合\(a,b,c,ab,ac,bc,all\)为\(p_A,p_B,p_C,p_A\capp_B,p_A\capp_A,p_B\capp_C,p_A\capp_B\capp_C\)。可以用bitset维护交集。首先加......
  • C语言 | 字符串函数(第二篇)【全网最详细,通俗易懂,必收藏】
    字符串函数(第二篇)一、strncpy1、strncpy的使用2、strncpy的模拟实现二、strncat1、strncat的使用2、strncat的模拟实现三、strncmp1、strncmp的使用2、strncmp的模拟实现四、strstr1、strstr的使用2、strstr的模拟实现一、strncpy1、strncpy的使用char*strnc......