C#开源实用的工具类库,集成超过1000多种扩展方法
前言
今天大姚给大家分享一个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;
}
}
支持在线搜索和演示
搜索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技术交流平台。无论您是初学者还是有丰富经验的开发者,我们都希望能为您提供更多的价值和成长机会。
C#开源实用的工具类库,集成超过1000多种扩展方法的更多相关文章
- 开源Math.NET基础数学类库使用(12)C#随机数扩展方法
原文:[原创]开源Math.NET基础数学类库使用(12)C#随机数扩展方法 本博客所有文章分类的总目录:http://www.cnblogs.com/asxinyu/p ...
- 【原创】开源Math.NET基础数学类库使用(12)C#随机数扩展方法
本博客所有文章分类的总目录:[总目录]本博客博文总目录-实时更新 开源Math.NET基础数学类库使用总目录:[目录]开源Math.NET基础数学类库使用总目录 前言 ...
- JavaScript实用的工具/类库
Moment.js//JavaScript日期处理类库. Lodash.js//非常实用的工具库. MD5//加密. Numeral.js//格式化和数字四则运算. Pure.css//CSS模块.
- 【目录】开源Math.NET基础数学类库使用总目录
本博客所有文章分类的总目录链接:[总目录]本博客博文总目录-实时更新 1.开源Math.NET数学组件文章 1.开源Math.NET基础数学类库使用(01)综合介绍 2.开源Math.NET ...
- [原创][开源]SunnyUI.Net, C# .Net WinForm开源控件库、工具类库、扩展类库、多页面开发框架
SunnyUI.Net, 基于 C# .Net WinForm 开源控件库.工具类库.扩展类库.多页面开发框架 Blog: https://www.cnblogs.com/yhuse Gitee: h ...
- Apache—dbutils开源JDBC工具类库简介
Apache—dbutils开源JDBC工具类库简介 一.前言 commons-dbutils 是 Apache 组织提供的一个开源 JDBC工具类库,它是对JDBC的简单封装,学习成本极低,并且使用 ...
- 国人开源了一款小而全的 Java 工具类库,厉害啊!!
最近栈长看到了一款小而全的 Java 工具类库:Hutool,Github 已经接近 14K Star 了,想必一定很优秀,现在推荐给大家,很多轮子不要再造了! Hutool 是什么 Hutool 是 ...
- 编写更少量的代码:使用apache commons工具类库
Commons-configuration Commons-FileUpload Commons DbUtils Commons BeanUtils Commons CLI Commo ...
- [转载] 一共81个,开源大数据处理工具汇总(下),包括日志收集系统/集群管理/RPC等
原文: http://www.36dsj.com/archives/25042 接上一部分:一共81个,开源大数据处理工具汇总(上),第二部分主要收集整理的内容主要有日志收集系统.消息系统.分布式服务 ...
- Android 开源项目android-open-project工具库解析之(一) 依赖注入,图片缓存,网络相关,数据库orm工具包,Android公共库
一.依赖注入DI 通过依赖注入降低View.服务.资源简化初始化.事件绑定等反复繁琐工作 AndroidAnnotations(Code Diet) android高速开发框架 项目地址:https: ...
随机推荐
- etcd 历史版本回溯的方法
在使用 etcd 作为配置存储或者其他的场景,如果因为误操作对其中 key 的值进行了修改,如果想要找回原来的值,可以利用 etcd 的版本机制进行回溯找回以前的值.在具体操作之前,我们首先获取一下 ...
- C#微服务必学清单
在 C# 领域,有一些不错的微服务书籍和开源框架,对于学习微服务相关知识非常有帮助.以下是一些建议您阅读的微服务书目和开源框架. 微服务书目: 1. <Building Microservice ...
- SnakeYaml反序列化分析
前言 SnakeYaml是Java中解析yaml的库,而yaml是一种人类可读的数据序列化语言,通常用于编写配置文件等.yaml真是到哪都有啊. 环境搭建 <dependency> < ...
- JVM简明笔记4:垃圾回收
1 垃圾回收相关算法 垃圾回收器首先要做的就是,判断一个对象是存活状态还是死亡状态,死亡的对象将会被标识为垃圾数据并等待收集器进行清除. 判断一个对象是否为死亡状态的常用算法有两个:引用计数器算法 . ...
- 使用Docker搭建MongoDB 5.0版本副本集集群
1.mongodb集群 首先我们需要了解mongodb的集群模式,mongodb安装分为单机安装和集群安装.集群安装分为:主从复制(Master-Slaver)集群.副本集(Replica Set)集 ...
- 力扣1076(MySQL)-员工项目Ⅱ(简单)
题目: 编写一个SQL查询,报告所有雇员最多的项目. 查询结果格式如下所示: 解题思路: 方法一:将两个表联结,以project_id进行分组,统计员工数降序排序,然后筛选出第一条数据. 1 sel ...
- 先行一步,7大技术创新和突破,阿里云把 Serverless 领域的这些难题都给解了
简介: 函数计算 FC 首创 GPU 实例.业内首发实例级别可观测和调试.率先提供端云联调和多环境部署能力.GB 级别镜像启动时间优化至秒级.VPC 网络建连优化至200ms,Serverless ...
- MySQL深潜|剖析Performance Schema内存管理
简介: 本文主要是通过对PFS引擎的内存管理源码的阅读,解读PFS内存分配及释放原理,深入剖析其中存在的一些问题,以及一些改进思路. 一 引言 MySQL Performance schema(PF ...
- 工业视觉智能实战经验之IVI算法框架2.0
简介: 工业视觉智能团队在交付了多个工业视觉智能质检项目后,发现了工业视觉智能的共性问题和解法,打造了工业视觉智能平台,通过平台的方式积累和提升工业视觉的通用能力.在平台建设上最核心的能力是算法能力 ...
- [Linux] IP 地址配置, 网络地址配置文件
# 查看与配置网络状态命令 $ ifconfig # 临时设置 eth0 网卡的 IP 地址与子网掩码,netmask 可以省略 $ ifconfig eth0 192.168.0.100 netma ...