MasterServer.Lib.Utils
2.0.0
dotnet add package MasterServer.Lib.Utils --version 2.0.0
NuGet\Install-Package MasterServer.Lib.Utils -Version 2.0.0
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="MasterServer.Lib.Utils" Version="2.0.0" />
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="MasterServer.Lib.Utils" Version="2.0.0" />
<PackageReference Include="MasterServer.Lib.Utils" />
For projects that support Central Package Management (CPM), copy this XML node into the solution Directory.Packages.props file to version the package.
paket add MasterServer.Lib.Utils --version 2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
#r "nuget: MasterServer.Lib.Utils, 2.0.0"
#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.
#:package MasterServer.Lib.Utils@2.0.0
#:package directive can be used in C# file-based apps starting in .NET 10 preview 4. Copy this into a .cs file before any lines of code to reference the package.
#addin nuget:?package=MasterServer.Lib.Utils&version=2.0.0
#tool nuget:?package=MasterServer.Lib.Utils&version=2.0.0
The NuGet Team does not provide support for this client. Please contact its maintainers for support.
MasterServer.Lib.Utils
通用工具类库,提供常用的工具方法和扩展功能。 作者邮箱:dengjianhua1999@qq.com
特性
- 加密解密工具(MD5, Base64, AES等)
- 枚举操作工具
- 反射操作工具(深拷贝、属性操作等)
- 序列化工具(JSON, XML)
- 结果信息封装
- 类型转换和验证
核心组件
1. EncryptUtils
加密解密工具类,提供:
- MD5哈希(仅用于兼容旧系统,安全场景不推荐):
ComputeMD5(string input) - BCrypt 密码哈希:
HashPassword(string password, int workFactor = 12),VerifyPassword(string password, string hashedPassword) - Base64 编码/解码(编码,不是加密):
ToBase64(string input),FromBase64(string input) - AES-GCM 加密解密(推荐):
AesGcmEncrypt(string plaintext, string key),AesGcmDecrypt(string ciphertext, string key) - RSA-OAEP 公私钥加解密:
GetRsaKeyPair(),RsaEncrypt(string text, string publicKey),RsaDecrypt(string text, string privateKey) - 混合加密(RSA + AES-GCM):
ComplexEncrypt(string text, string publicKey),ComplexDecrypt(string encrypted, string privateKey)
2. EnumUtils
枚举操作工具类,提供:
- 解析枚举:
Parse<T>()- 获取枚举键值对列表 - 获取描述:
GetDescription()- 获取枚举项的Description特性 - 字符串转换:
ParseFromString<T>()- 从字符串解析枚举
3. ReflectionUtils
反射操作工具类,提供:
- 深拷贝:
DeepCopyProperties()- 对象属性深拷贝 - 属性操作:
GetPropertyValue(),SetPropertyValue() - 类型检查:
IsSimpleType(),IsCollectionType()
4. Serializer
序列化工具类,提供:
- JSON序列化:
ToJson(),FromJson() - XML序列化:
ToXml(),FromXml() - 二进制序列化:
ToBinary(),FromBinary()
5. ResultInfo<T>
结果信息封装类,提供:
- 成功结果:
Success(T data, string message) - 失败结果:
Fail(string message, int code) - 统一结果处理: 包含状态码、消息和数据
使用方式
1. 加密解密
using MasterServer.Lib.Utils;
// MD5哈希
string hash = EncryptUtils.ComputeMD5("password");
// Base64编码
string encoded = EncryptUtils.ToBase64("Hello World");
string decoded = EncryptUtils.FromBase64(encoded);
// AES加密
string key = "your-secret-key";
string encrypted = EncryptUtils.EncryptAES("sensitive data", key);
string decrypted = EncryptUtils.DecryptAES(encrypted, key);
2. 枚举操作
public enum UserStatus
{
[Description("活跃用户")]
Active = 1,
[Description("禁用用户")]
Disabled = 2
}
// 解析枚举
var enumList = EnumUtils.Parse<UserStatus>();
// 返回: [{1, "活跃用户"}, {2, "禁用用户"}]
// 从字符串解析
UserStatus status = EnumUtils.ParseFromString<UserStatus>("Active");
3. 反射操作
// 深拷贝对象
var source = new MyClass { Name = "Test", Value = 123 };
var target = new MyClass();
ReflectionUtils.DeepCopyProperties(source, target);
// 获取属性值
object value = ReflectionUtils.GetPropertyValue(obj, "PropertyName");
4. 序列化
var data = new { Name = "Test", Value = 123 };
// JSON序列化
string json = Serializer.ToJson(data);
var deserialized = Serializer.FromJson<MyClass>(json);
// XML序列化
string xml = Serializer.ToXml(data);
var fromXml = Serializer.FromXml<MyClass>(xml);
5. 结果封装
// 成功结果
ResultInfo<string> success = ResultInfo<string>.Success("操作成功", "数据内容");
// 失败结果
ResultInfo<string> fail = ResultInfo<string>.Fail("操作失败", 500);
// 检查结果
if (result.IsSuccess)
{
Console.WriteLine(result.Message);
var data = result.Data;
}
安装
通过NuGet安装:
Install-Package MasterServer.Lib.Utils
依赖
- System.ComponentModel
- System.Reflection
- System.Runtime.Serialization
- Newtonsoft.Json (可选,用于JSON序列化)</content> <parameter name="filePath">d:\work\C#Work\2025\MasterServerlib2\MasterServer.Lib.Utils\README.md
| Product | Versions Compatible and additional computed target framework versions. |
|---|---|
| .NET | net5.0 was computed. net5.0-windows was computed. net6.0 was computed. 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 was computed. 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. net9.0 was computed. net9.0-android was computed. net9.0-browser was computed. net9.0-ios was computed. net9.0-maccatalyst was computed. net9.0-macos was computed. net9.0-tvos was computed. net9.0-windows was computed. net10.0 was computed. net10.0-android was computed. net10.0-browser was computed. net10.0-ios was computed. net10.0-maccatalyst was computed. net10.0-macos was computed. net10.0-tvos was computed. net10.0-windows was computed. |
| .NET Core | netcoreapp3.0 was computed. netcoreapp3.1 was computed. |
| .NET Standard | netstandard2.1 is compatible. |
| MonoAndroid | monoandroid was computed. |
| MonoMac | monomac was computed. |
| MonoTouch | monotouch was computed. |
| Tizen | tizen60 was computed. |
| Xamarin.iOS | xamarinios was computed. |
| Xamarin.Mac | xamarinmac was computed. |
| Xamarin.TVOS | xamarintvos was computed. |
| Xamarin.WatchOS | xamarinwatchos was computed. |
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
-
.NETStandard 2.1
- BCrypt.Net-Next (>= 4.1.0)
- Newtonsoft.Json (>= 13.0.4)
NuGet packages
This package is not used by any NuGet packages.
GitHub repositories
This package is not used by any popular GitHub repositories.