偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

C#開源實用的工具類庫,集成超過1000多種擴展方法

開發(fā) 開發(fā)工具
今天大姚給大家分享一個C#開源(MIT License)、免費、實用且強大的工具類庫,集成超過1000多種擴展方法增強 .NET Framework 和 .NET Core的使用效率: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字節(jié)數(shù)組

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);
                }
            }
        }
    }
}

將泛型數(shù)組轉(zhuǎn)換為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在線演示:

圖片 圖片

責任編輯:武曉燕 來源: 追逐時光者
相關推薦

2024-07-08 11:58:19

2024-10-31 10:18:43

C#ORM框架

2009-08-10 17:36:17

C#擴展方法

2009-08-27 18:04:01

c#擴展方法string

2009-09-01 11:04:59

C#調(diào)用擴展方法

2009-08-31 14:45:10

C#擴展方法

2009-08-05 14:54:09

VB調(diào)用C#類庫

2009-08-26 15:53:48

C#擴展方法

2009-08-18 14:14:45

C#擴展方法性能測試

2009-08-27 09:27:49

C#擴展方法

2009-08-27 16:24:48

擴展方法C# 3.0新特性

2009-08-20 18:21:04

GetType方法C# object類

2025-02-10 10:52:34

WinForm窗口閃爍C#

2011-02-21 16:11:45

C#.NET.NET framew

2009-09-01 10:20:28

C#多種語句

2009-08-13 17:06:37

C#擴展方法Enumerable.

2009-08-28 16:29:02

C#類庫工程

2009-04-03 13:20:05

C#擴展方法調(diào)用

2009-08-13 17:25:18

C# Convert類

2009-07-30 15:35:47

C#時間函數(shù)
點贊
收藏

51CTO技術棧公眾號