C#开源实用的工具类库,集成超过1000多种扩展方法
【摘要】 前言今天大姚给大家分享一个C#开源(MIT License)、免费、实用且强大的工具类库,集成超过1000多种扩展方法增强 .NET Framework 和 .NET Core的使用效率:Z.ExtensionMethods。直接项目引入类库使用在你的对应项目中NuGet包管理器中搜索:Z.ExtensionMethods安装即可使用。支持.NET Standard 2.0和.NET Fra...
前言
今天大姚给大家分享一个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/
【版权声明】本文为华为云社区用户原创内容,转载时必须标注文章的来源(华为云社区)、文章链接、文章作者等基本信息, 否则作者和本社区有权追究责任。如果您发现本社区中有涉嫌抄袭的内容,欢迎发送邮件进行举报,并提供相关证据,一经查实,本社区将立刻删除涉嫌侵权内容,举报邮箱:
cloudbbs@huaweicloud.com
- 点赞
- 收藏
- 关注作者
评论(0)