Xunet 1.0.13

There is a newer version of this package available.
See the version list below for details.
dotnet add package Xunet --version 1.0.13                
NuGet\Install-Package Xunet -Version 1.0.13                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="Xunet" Version="1.0.13" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Xunet --version 1.0.13                
#r "nuget: Xunet, 1.0.13"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install Xunet as a Cake Addin
#addin nuget:?package=Xunet&version=1.0.13

// Install Xunet as a Cake Tool
#tool nuget:?package=Xunet&version=1.0.13                

常用工具库

Xunet 是 .NET 的通用工具库,包含扩展方法、辅助类和一些常用组件,用于简化开发和提高工作效率。

支持 .NET Framework 4.5、.NET 6.0、.NET 7.0

Xunet Xunet

扩展类

1、日期时间扩展类

// 获取当前时间戳
var timeStamp = DateTime.Now.ToTimeStamp();
// 时间戳转DateTime
var dateTime = timeStamp.ToDateTime();

2、Ffmpeg扩展类

// 音频文件路径
var audio_path = "音频文件路径";
// 获取音频文件时长
var length = audio_path.GetMediaLength("Ffmpeg.exe所在目录,默认当前目录");
// 视频文件路径
var video_path = "视频文件路径";
// 获取视频截图
var image = video_path.CatchVideoImg("目标存储路径", "Ffmpeg.exe所在目录,默认当前目录");

3、HttpClient扩展类

// 接口地址
var url = "https://api.demo.com/test";
// GET请求
var result = await url.HttpGetAsync();
// POST请求(支持application/json和form-data)
var result2 = await url.HttpPostAsync(HttpContentType.ApplicationJson, 
new { id = "参数1", name = "参数2" });
// PUT请求(支持application/json和form-data)
var result2 = await url.HttpPutAsync(HttpContentType.ApplicationJson, 
new { id = "参数1", name = "参数2" });
// DELETE请求
var result = await url.HttpDeleteAsync();

4、Json扩展类

// object对象
var data = new { code = 0, message = "" };
// JSON序列化
var json = data.SerializeObject();
// JSON反序列化
var obj = json.DeserializeObject<object>();

5、随机数扩展类

// 获取0~10以内的随机数(方式一)
var randomNum1 = 0.Next(10);
// 获取0~10以内的随机数(方式二)
var randomNum2 = 10.Next();
// 获取8位数随机字符串
// 参数:level,复杂等级,0纯数字,1数字+字母,2数字+字母+特殊字符
var randomText = 8.NextString(2);

6、加解密扩展类

// 输入字符串
var input = "123456";
// MD5加密
var md5Value = input.ToMD5Encrypt();
// SHA256加密
var sha256Value = input.ToSHA256Encrypt();
// AES加密
var aesValue = input.ToAESEncrypt();
// AES解密
var aesText = aesValue.ToAESDecrypt();
// RSA加密
// 获取RSA公钥和私钥(2048位)
var key = 2048.ToRSASecretKey();
// 公钥加密
var rsaValue = input.ToRSAEncrypt(key.PublicKey);
// 私钥解密
var rsaText = rsaValue.ToRSADecrypt(key.PrivateKey);

7、String扩展类

// 输入字符串
var input = "123456";
// 判断是否是null或者空字符串
var isNull = input.IsNullOrEmpty();
// 判断是否非null且非空字符串
var isNotNull = input.IsNotNullOrEmpty();
// 判断是否是手机号
var isTel = input.IsPhoneNumber();
// ...

8、反射扩展类

// 对象
var user = new User { Name = "徐来" };
// 获取Name属性
var name = user.GetProperty<string>("Name");
// 设置Name属性
var namenew = user.SetProperty("Name", "徐来new");

9、异常处理扩展类

var result = typeof(TestClass).TryCatch(
    (x) =>
    {
        // 主要业务逻辑
    },
    (x) =>
    {
        // 异常处理
    },
    (x) =>
    {
        // 执行成功
    });

typeof(TestClass).TryFinally(
    (x) => 
    {
        // 主要业务逻辑
    },
    (x) => 
    {
        // finally处理
    });

辅助类

1、雪花ID

// 雪花Id(long类型)
var id1 = SnowflakeHelper.NextId();
// 雪花Id(字符串)
var id2 = SnowflakeHelper.NextIdString();

2、拼音助手

// 获取中文全拼
var pinyin = PinyinHelper.GetPinyin("徐来");

3、读取配置文件

// 获取配置
var value = AppSettingsHelper.GetValueOrDefault("key");

4、性能计时器

var milliSeconds = StopwatchHelper.Execute(() =>
{
    // do something
    Thread.Sleep(100);
});

5、数据计算

// 获取两个坐标的距离
var distance = MathHelper.GetDistance(1, 2, 3, 4);

6、IP转地区

// 说明:仅支持.NET 6.0 及以上版本
// 需要依赖ip2region.xdb文件
// https://github.com/lionsoul2014/ip2region/blob/master/data/ip2region.xdb

// 根据IP查询地区
var region = IP2RegionHelper.Search("ip地址");

7、钉钉

// 发送应用消息
var access_token = DingtalkHelper.GetAccessToken("appkey", "appsecret");
DingtalkHelper.SendMarkdownMessage(new SendMarkdownMessageRequest
{
    AgentId = 123456,
    AccessToken = access_token,
    Title = "恐龙抗狼",
    Text = "我没K,我没K,我没K,布鲁布鲁biu~",
    UserIds = new List<string> { "userId001" }
});

// 发送群机器人消息
DingtalkHelper.SendMarkdownMessageForGroupRobot(new SendMarkdownMessageForGroupRobotRequest
{
    Webhook = "",
    Secret = "",
    Title = "恐龙抗狼",
    Text = "我没K,我没K,我没K,布鲁布鲁biu~",
    AtUserIds = new List<string> { "userId001" }
});

8、Redis

// 初始化Redis缓存
RedisHelper.Initialization(new CSRedis.CSRedisClient("127.0.0.1:6379"));
// 设置缓存
await RedisHelper.SetAsync("user", new User { Name = "徐来" });
// 获取缓存
var user = await RedisHelper.GetAsync<User>("user");

9、机器

// 获取本机IP地址
var localIPAddress = MachineHelper.GetLocalIPAddress();
// 获取公网IP地址
var publicIPAddress = MachineHelper.GetPublicIPAddress();
// 获取机器码
var machineCode = MachineHelper.GetMachineCode();
// 获取随机端口号
var port = MachineHelper.GetRandomPort();
// 检查端口号是否被占用
var isPortInUse = MachineHelper.IsPortInUse(port);
// 获取当前机器的注册码
var registerCode = MachineHelper.GetRegisterCode("32位秘钥", 30);
// 验证注册码是否有效
var verifyRegisterCode = MachineHelper.VerifyRegisterCode("32位秘钥", registerCode);

组件

1、日志组件

LogManager.Info("这是一条测试日志");

2、缓存组件

// 使用CSRedis;
using Xunet.CSRedis;

// 初始化Redis缓存
RedisHelper.Initialization(new CSRedis.CSRedisClient("127.0.0.1:6379"));
// 设置缓存
await RedisHelper.SetAsync("user", new User { Name = "徐来" });
// 获取缓存
var user = await RedisHelper.GetAsync<User>("user");

3、ORM组件

// 使用Dapper
using Xunet.Dapper;

// 查询
var list = await connection.QueryAsync<User>("select * from user;");

感谢

Product Compatible and additional computed target framework versions.
.NET net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Framework net45 is compatible.  net451 was computed.  net452 was computed.  net46 was computed.  net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on Xunet:

Package Downloads
Xunet.WinFormium

A Lightweight WinForm Application Framework For .NET

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.0.23 124 8/16/2024
1.0.22 111 7/3/2024
1.0.21 94 6/28/2024
1.0.20 81 6/25/2024
1.0.19 120 4/10/2024
1.0.18 126 12/15/2023
1.0.17 124 11/28/2023
1.0.16 129 11/27/2023
1.0.15 114 11/27/2023
1.0.14 141 8/8/2023
1.0.13 161 8/7/2023
1.0.12 183 7/13/2023
1.0.11 153 7/4/2023
1.0.10 166 6/12/2023
1.0.9 152 6/8/2023
1.0.8 167 6/8/2023
1.0.7 214 6/7/2023
1.0.6 160 6/6/2023
1.0.5 151 6/6/2023
1.0.4 160 6/5/2023
1.0.3 136 6/5/2023
1.0.2 158 6/5/2023