前言
今天大姚给大家分享一个C#开源(MIT License)、免费、实用且强大的工具类库,集成超过1000多种扩展方法增强 .NET Framework 和 .NET Core的使用效率:Z.ExtensionMethods。
直接项目引入类库使用
在你的对应项目中NuGet包管理器中搜索:Z.ExtensionMethods
安装即可使用。
项目源代码
部分扩展方法展示
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;
}
}
支持在线搜索和演示
搜索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推荐或自荐(让优秀的项目和框架不被埋没🤞)。
DotNetGuide技术社区交流群
- DotNetGuide技术社区是一个面向.NET开发者的开源技术社区,旨在为开发者们提供全面的C#/.NET/.NET Core相关学习资料、技术分享和咨询、项目框架推荐、求职和招聘资讯、以及解决问题的平台。
- 在DotNetGuide技术社区中,开发者们可以分享自己的技术文章、项目经验、学习心得、遇到的疑难技术问题以及解决方案,并且还有机会结识志同道合的开发者。
- 我们致力于构建一个积极向上、和谐友善的.NET技术交流平台。无论您是初学者还是有丰富经验的开发者,我们都希望能为您提供更多的价值和成长机会。