当前位置: 首页> 房产> 建材 > C#开源实用的工具类库,集成超过1000多种扩展方法

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

时间:2025/7/9 7:58:22来源:https://blog.csdn.net/qq_37237487/article/details/139375092 浏览次数:0次

前言

今天大姚给大家分享一个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 bytesusing (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 bytesusing (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在线演示:

图片

项目源码地址

更多项目实用功能和特性欢迎前往项目开源地址查看👀,别忘了给项目一个Star支持💖。

  • GitHub开源地址:https://github.com/zzzprojects/Z.ExtensionMethods

  • 在线搜索和演示地址:https://csharp-extension.com/en/online-example/

优秀项目和框架精选

该项目已收录到C#/.NET/.NET Core优秀项目和框架精选中,关注优秀项目和框架精选能让你及时了解C#、.NET和.NET Core领域的最新动态和最佳实践,提高开发工作效率和质量。坑已挖,欢迎大家踊跃提交PR推荐或自荐(让优秀的项目和框架不被埋没🤞)。

  • https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.md

图片

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

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com

责任编辑: