【技术干货】开源库 Com.Gitusme.Net.Extensiones.Core 的使用

目录

1、项目介绍

2、为项目添加依赖

3、代码中导入命名空间

4、代码中使用

[示例 1:string转换](#示例 1:string转换)

[示例 2:object转换](#示例 2:object转换)


1、项目介绍

Com.Gitusme.Net.Extensiones.Core是一个.Net扩展库。当前最新版本1.0.4,提供了常见类型转换,可减少代码量,并提高编程效率。基于.Net Standard 2.0开发,支持windows、linux、IOS等多平台。

2、为项目添加依赖

打开NuGet包管理,搜索"Com.Gitusme.Net.Extensiones.Core"关键字,安装最新版本。

3、代码中导入命名空间

using Com.Gitusme.Net.Extensions.Core;

4、使用示例

示例 1:string转换

// Example 1: 判断string是否为null

string str = null;

if (str.IsNullOrEmpty())

{

System.Console.WriteLine("Example 1 输出结果:" + "null");

}

// Example 2: 判断string是否匹配正则

string hello = "Hello, gitusme";

var isMatch = hello.IsMatch(@"Hello, \w+");

System.Console.WriteLine("Example 2 输出结果:" + isMatch);

// Example 3: 将string转换为Json实体对象

string json = "{\"Id\":0,\"Name\":\"Json Object\"}";

var jsonObj = json.ToJsonObject<MyJsonObject>();

System.Console.WriteLine("Example 3 输出结果:" + jsonObj.Name);

// Example 4: 将string转换为Xml实体对象

string xml = "<?xml version=\"1.0\" encoding=\"utf-8\"?>" +

"<root xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\\" xmlns:xsd=\"http://www.w3.org/2001/XMLSchema\\" id=\"0\">" +

"<name>Xml Object</name>" +

"</root>";

var xmlObj = xml.ToXmlObject<MyXmlObject>();

System.Console.WriteLine("Example 4 输出结果:" + xmlObj.Name);

// Example 5: 将string转换为DateTime

string date = "2023/10/22 21:32:00";

var dateTime = date.ToDateTime();

System.Console.WriteLine("Example 5 输出结果:" + dateTime.ToString());

// Example 6: 将string转换为字符数组

string gitusme = "gitusme";

var array = gitusme.ToCharArray();

System.Console.WriteLine("Example 6 输出结果:" + array);

// Example 7: 将string转换为int32

string intStr = "100";

var intValue = intStr.ToInt32() + 10;

System.Console.WriteLine("Example 7 输出结果:" + intValue);

// Example 8: 将string转换为decimal

string decimalStr = "3.141592653589793238462643383279502884197";

var decimalValue = decimalStr.ToDecimal();

System.Console.WriteLine("Example 8 输出结果:" + decimalValue);

// Example 9: 将string转换为uri

string uriStr = "https://github.com/gitusme/Com.Gitusme.Net.Extensiones";

var uriValue = uriStr.ToUri();

System.Console.WriteLine("Example 9 输出结果:" + uriValue.Host);

// Example 10: 将string转换为color

string colorStr = "#aabbcc";

var colorValue = colorStr.ToColor();

System.Console.WriteLine("Example 10 输出结果:" + colorValue);

运行结果:

示例 2:object转换

// Example 1: 判断对象是否为null

MyObject obj = null;

if (obj.IsNull())

{

System.Console.WriteLine("Example 1 输出结果:" + "null");

}

// Example 2: 对象为null的时候,返回传入的默认值

var def = obj.OrDefault(new MyObject());

System.Console.WriteLine("Example 2 输出结果:" + def);

// Example 3: 如果对象不为null,则执行传入的Action行为

def.IfPresent((it) =>

{

System.Console.WriteLine("Example 3 输出结果:" + it.Name);

});

// Example 4: 将Xml对象转换为Xml文本

var xmlObj = new MyXmlObject();

string xml = xmlObj.ToXml();

System.Console.WriteLine("Example 4 输出结果:" + xml);

// Example 5: 将Json对象转换为Json文本

var jsonObj = new MyJsonObject();

string json = jsonObj.ToJson();

System.Console.WriteLine("Example 5 输出结果:" + json);

运行结果:

您能够认真学习完本篇,实属不易,欢迎点赞加关注,收藏交流和探讨。

相关推荐
猿究院--Cu-Sn合金36 分钟前
DeepSeek Harness 0.1.1-rc.1 发布:我的插件和启动器已完成适配
开源·deepseek
冬奇Lab2 小时前
开源项目第194期:deepseek-harness — DeepSeek 出品的 AI Agent 开发框架,万物皆插件
人工智能·开源·deepseek
一次旅行3 小时前
2026‑08‑21 AI产业深度解读|GUI智能体、投机解码、十亿级向量检索、国产Base模型开源
人工智能·开源
呆呆敲代码的小Y3 小时前
10 分钟搞懂 cua:开源 AI 操作电脑基础设施,附 Python 沙箱与 Agent 上手代码
人工智能·python·开源·ai agent·awesome·llm应用·cua
分布式存储与RustFS3 小时前
从 MinIO 到 RustFS:不只是换语言,更是换一份协议自由
云原生·开源·对象存储·分布式存储·s3·rustfs·性能基准
longxiaozhang64 小时前
C#继承:让代码少写一点,偷懒的好方法
开发语言·c#
小妖同学学AI4 小时前
60.8k星!开源金融数据神器OpenBB:连接一切数据,让分析师、量化交易员和AI智能体如虎添翼!
人工智能·金融·开源
网安蟹佬霸5 小时前
密码学安全实战:从加密原理到哈希破解的完整攻防指南
网络·算法·安全·web安全·开源·密码学·哈希算法
rockey6275 小时前
C#脚本引擎之AScript与DynamicExpresso对比
c#·.net·script·动态脚本
前进的程序员5 小时前
告别Copilot?Codex本地化部署指南
开源·php·copilot·codex