AVt天堂网 手机版,亚洲va久久久噜噜噜久久4399,天天综合亚洲色在线精品,亚洲一级Av无码毛片久久精品

當(dāng)前位置:首頁 > 科技  > 軟件

C#開源實(shí)用的工具類庫,集成超過1000多種擴(kuò)展方法

來源: 責(zé)編: 時(shí)間:2024-05-30 09:11:21 186觀看
導(dǎo)讀前言今天大姚給大家分享一個(gè)C#開源(MIT License)、免費(fèi)、實(shí)用且強(qiáng)大的工具類庫,集成超過1000多種擴(kuò)展方法增強(qiáng) .NET Framework 和 .NET Core的使用效率:Z.ExtensionMethods。直接項(xiàng)目引入類庫使用在你的對(duì)應(yīng)項(xiàng)目中NuGet包

前言

今天大姚給大家分享一個(gè)C#開源(MIT License)、免費(fèi)、實(shí)用且強(qiáng)大的工具類庫,集成超過1000多種擴(kuò)展方法增強(qiáng) .NET Framework 和 .NET Core的使用效率:Z.ExtensionMethods。KnF28資訊網(wǎng)——每日最新資訊28at.com

直接項(xiàng)目引入類庫使用

在你的對(duì)應(yīng)項(xiàng)目中NuGet包管理器中搜索:Z.ExtensionMethods安裝即可使用。KnF28資訊網(wǎng)——每日最新資訊28at.com

支持.NET Standard 2.0和.NET Framework 4.0 。KnF28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片KnF28資訊網(wǎng)——每日最新資訊28at.com

項(xiàng)目源代碼

圖片圖片KnF28資訊網(wǎng)——每日最新資訊28at.com

部分?jǐn)U展方法展示

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/KnF28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片KnF28資訊網(wǎng)——每日最新資訊28at.com

搜索ToMD5Hash:KnF28資訊網(wǎng)——每日最新資訊28at.com

圖片圖片KnF28資訊網(wǎng)——每日最新資訊28at.com

使用.NET Fiddle在線演示:KnF28資訊網(wǎng)——每日最新資訊28at.com

圖片 圖片 KnF28資訊網(wǎng)——每日最新資訊28at.com

本文鏈接:http://www.tebozhan.com/showinfo-26-91685-0.htmlC#開源實(shí)用的工具類庫,集成超過1000多種擴(kuò)展方法

聲明:本網(wǎng)頁內(nèi)容旨在傳播知識(shí),若有侵權(quán)等問題請(qǐng)及時(shí)與本網(wǎng)聯(lián)系,我們將在第一時(shí)間刪除處理。郵件:2376512515@qq.com

上一篇: 面試官:說說Netty核心組件?

下一篇: 尤雨溪:這個(gè)前端經(jīng)典輪子值得去造!

標(biāo)簽:
  • 熱門焦點(diǎn)
Top