Converter.Extention 2.0.0

dotnet add package Converter.Extention --version 2.0.0
                    
NuGet\Install-Package Converter.Extention -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="Converter.Extention" Version="2.0.0" />
                    
For projects that support PackageReference, copy this XML node into the project file to reference the package.
<PackageVersion Include="Converter.Extention" Version="2.0.0" />
                    
Directory.Packages.props
<PackageReference Include="Converter.Extention" />
                    
Project file
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 Converter.Extention --version 2.0.0
                    
#r "nuget: Converter.Extention, 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 Converter.Extention@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=Converter.Extention&version=2.0.0
                    
Install as a Cake Addin
#tool nuget:?package=Converter.Extention&version=2.0.0
                    
Install as a Cake Tool

Converter.Extension

NuGet Downloads License

一个高性能的 .NET 类型转换和 JSON 序列化扩展库,支持多版本框架兼容性。

✨ 特性

  • 🚀 高性能类型转换 - 支持所有基础类型的安全转换
  • 📦 JSON 序列化 - 支持多版本 .NET 框架的 JSON 操作
  • 🔄 多框架兼容 - 支持 .NET Framework 4.5、.NET Standard 2.0、.NET 5.0+
  • 🛡️ 异常安全 - 提供安全转换和异常处理机制
  • 扩展方法 - 直观易用的扩展方法设计
  • 🎯 泛型支持 - 强类型的泛型转换方法

📦 安装

Package Manager

Install-Package Converter.Extention

.NET CLI

dotnet add package Converter.Extention

PackageReference

<PackageReference Include="Converter.Extention" Version="2.0.0" />

🎯 支持的目标框架

  • .NET Framework 4.5+
  • .NET Standard 2.0
  • .NET 5.0+

🚀 快速开始

基础类型转换

using Converter.Extension;

// 字符串转换为各种基础类型
string value = "123";

// 转换为 int(失败时抛出异常)
int number = value.ConvertToInt();

// 转换为 int(失败时返回默认值)
int numberWithDefault = value.ConvertToInt(defaultValue: 0);

// 支持的类型转换
bool boolValue = "true".ConvertToBool();
byte byteValue = "255".ConvertToByte();
short shortValue = "32767".ConvertToShort();
ushort ushortValue = "65535".ConvertToUShort();
uint uintValue = "4294967295".ConvertToUInt();
long longValue = "9223372036854775807".ConvertToLong();
ulong ulongValue = "18446744073709551615".ConvertToULong();
float floatValue = "3.14".ConvertToFloat();
double doubleValue = "3.14159265359".ConvertToDouble();
decimal decimalValue = "99999999.99".ConvertToDecimal();
DateTime dateValue = "2023-12-25".ConvertToDateTime();
char charValue = "A".ConvertToChar();

泛型转换

// 泛型转换方法
string value = "42";

// 转换为指定类型(失败时抛出异常)
int result = value.ConvertToT<int>();

// 转换为指定类型(失败时返回默认值)
int resultWithDefault = value.ConvertToT<int>(defaultValue: 0);

// 支持枚举类型
enum Status { Active, Inactive }
Status status = "Active".ConvertToT<Status>();

JSON 序列化(如果包含 SerializeHelper)

// 对象序列化
var person = new { Name = "张三", Age = 25, Email = "zhangsan@example.com" };
string json = person.ToJson();

// 格式化序列化
string formattedJson = person.ToJson(indented: true);

// 反序列化
var result = json.FromJson<Person>();

// 安全反序列化
if (json.TryFromJson<Person>(out var person2))
{
    Console.WriteLine($"姓名: {person2.Name}");
}

// 带默认值的反序列化
var personOrDefault = json.FromJsonOrDefault<Person>();

// JSON 验证
bool isValid = json.IsValidJson();

// JSON 格式化
string formatted = json.FormatJson();

// JSON 压缩
string compact = json.CompactJson();

// 深拷贝
var clone = person.DeepClone();

📚 API 文档

ConverterHelper 类

基础类型转换方法
方法 描述 返回类型 异常处理
ConvertToBool() 转换为布尔值 bool 抛出异常
ConvertToBool(defaultValue) 转换为布尔值 bool 返回默认值
ConvertToByte() 转换为字节 byte 抛出异常
ConvertToByte(defaultValue) 转换为字节 byte 返回默认值
ConvertToShort() 转换为短整型 short 抛出异常
ConvertToShort(defaultValue) 转换为短整型 short 返回默认值
ConvertToInt() 转换为整型 int 抛出异常
ConvertToInt(defaultValue) 转换为整型 int 返回默认值
ConvertToLong() 转换为长整型 long 抛出异常
ConvertToLong(defaultValue) 转换为长整型 long 返回默认值
ConvertToFloat() 转换为单精度浮点 float 抛出异常
ConvertToFloat(defaultValue) 转换为单精度浮点 float 返回默认值
ConvertToDouble() 转换为双精度浮点 double 抛出异常
ConvertToDouble(defaultValue) 转换为双精度浮点 double 返回默认值
ConvertToDecimal() 转换为十进制数 decimal 抛出异常
ConvertToDecimal(defaultValue) 转换为十进制数 decimal 返回默认值
ConvertToDateTime() 转换为日期时间 DateTime 抛出异常
ConvertToDateTime(defaultValue) 转换为日期时间 DateTime 返回默认值
ConvertToChar() 转换为字符 char 抛出异常
ConvertToChar(defaultValue) 转换为字符 char 返回默认值
泛型转换方法
方法 描述 返回类型 异常处理
ConvertToT<T>() 泛型转换 T 抛出异常
ConvertToT<T>(defaultValue) 泛型转换 T 返回默认值

SerializeHelper 类(如果包含)

JSON 序列化方法
方法 描述 返回类型
ToJson<T>() 对象转 JSON string
ToJson<T>(indented) 格式化序列化 string
FromJson<T>() JSON 转对象 T
TryFromJson<T>(out result) 安全反序列化 bool
FromJsonOrDefault<T>() 带默认值反序列化 T
IsValidJson() 验证 JSON 格式 bool
FormatJson() 格式化 JSON string
CompactJson() 压缩 JSON string
DeepClone<T>() 深拷贝对象 T

🔧 高级特性

多框架兼容性

该库使用条件编译来确保在不同 .NET 版本中的最佳性能:

  • .NET 5.0+: 使用高性能的 System.Text.Json
  • .NET Standard 2.0/.NET Framework: 使用稳定的 Newtonsoft.Json

性能优化

  • 使用内联方法优化([MethodImpl(MethodImplOptions.AggressiveInlining)]
  • 最小化装箱/拆箱操作
  • 优化的异常处理机制

📝 示例项目

using System;
using Converter.Extension;

class Program
{
    static void Main()
    {
        // 基础类型转换示例
        Console.WriteLine("=== 基础类型转换 ===");
      
        string[] testValues = { "123", "true", "3.14", "2023-12-25", "invalid" };
      
        foreach (var value in testValues)
        {
            Console.WriteLine($"原始值: {value}");
          
            // 安全转换(使用默认值)
            var intResult = value.ConvertToInt(0);
            var boolResult = value.ConvertToBool(false);
            var doubleResult = value.ConvertToDouble(0.0);
          
            Console.WriteLine($"  转换为 int: {intResult}");
            Console.WriteLine($"  转换为 bool: {boolResult}");
            Console.WriteLine($"  转换为 double: {doubleResult}");
            Console.WriteLine();
        }
      
        // 泛型转换示例
        Console.WriteLine("=== 泛型转换 ===");
      
        string numberStr = "42";
        var number = numberStr.ConvertToT<int>();
        Console.WriteLine($"泛型转换结果: {number}");
      
        // JSON 序列化示例(如果包含 SerializeHelper)
        Console.WriteLine("=== JSON 序列化 ===");
      
        var data = new { Name = "测试", Value = 100, Items = new[] { 1, 2, 3 } };
        var json = data.ToJson();
        Console.WriteLine($"序列化结果: {json}");
      
        // 验证 JSON
        Console.WriteLine($"JSON 有效性: {json.IsValidJson()}");
    }
}

🤝 贡献

欢迎贡献代码!请遵循以下步骤:

  1. Fork 本仓库
  2. 创建功能分支 (git checkout -b feature/AmazingFeature)
  3. 提交更改 (git commit -m 'Add some AmazingFeature')
  4. 推送到分支 (git push origin feature/AmazingFeature)
  5. 创建 Pull Request

📋 变更日志

[2.0.0] - 2024-12-XX

  • ✨ 添加高级 JSON 序列化功能
  • 🚀 性能优化和内存使用改进
  • 🔧 改进多框架兼容性
  • 📚 完善文档和示例

[1.1.0] - 2024-XX-XX

  • ✨ 添加泛型转换方法
  • 🐛 修复类型转换边界情况
  • 📝 改进文档

[1.0.0] - 2024-XX-XX

  • 🎉 初始版本发布
  • ✨ 基础类型转换功能

📄 许可证

本项目基于 MIT 许可证 开源。

🔗 相关链接

👨‍💻 作者

MarkLin - GitHub


如果这个库对您有帮助,请给个 ⭐ Star!

Product Compatible and additional computed target framework versions.
.NET net5.0 is compatible.  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 netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 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. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  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.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last Updated
2.0.0 156 9/26/2025