zijian666.Converts 5.3.2.1

dotnet add package zijian666.Converts --version 5.3.2.1                
NuGet\Install-Package zijian666.Converts -Version 5.3.2.1                
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="zijian666.Converts" Version="5.3.2.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add zijian666.Converts --version 5.3.2.1                
#r "nuget: zijian666.Converts, 5.3.2.1"                
#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 zijian666.Converts as a Cake Addin
#addin nuget:?package=zijian666.Converts&version=5.3.2.1

// Install zijian666.Converts as a Cake Tool
#tool nuget:?package=zijian666.Converts&version=5.3.2.1                

Fork me on Gitee

Converts

超级转换器

.NET Standard 2.0 重新设计,同时支持.NET4.5

更新日志

  • Converts
    主项目,提供各种类型转换的入口及注册
  • Converts.Json
    JSON支持,支持各种类型与json字符串之间的互转
    var json = user.To<string>()
    var user = """{"id":1,"name":"zijian666"}""".To<User>()
  • Converts.Dynamic 动态类型支持,例如键值对,匿名类或json字符串转dynamic, 转动态类型后属性不区分大小写
    var dy = new { id = 1, name = "zijian666" }.ToDynamic();// dy.ID == 1
  • Converts.Mapping 可注册任意类型属性映射
    ConvertsMapping.Map<MyClass1, MyClass2>((m1, m2) => m2.Name = m1.NickName);

说明

对象转换,从未如此简单

obj.To<T>();                //转换失败,抛出异常
obj.To<T>(T defaultValue);  //转换失败,返回默认值

代码展示

<details> <summary>示例代码,点击展开</summary>

//最基本
"1".To<int>();
"a".To<int>(0); //转换失败返回 0
"是".To<bool>(); //支持 "是/否" "真/假" "对/错" "t/f" "true/false" 等
byte[].To<Guid>();
"2018-02-20 16:50:02".To<int>(); // 支持时间字符串转时间戳
1519116602L.To<DateTime>();      // 支持秒级时间戳转时间
1519116602000L.To<DateTime>();   // 支持毫秒级时间戳转时间(自动判断)


//进阶
"1,2,3,4,5,6".To<int[]>();
Dictionary.To<Entity>(); //键值对转实体
DataRow.To<Entity>(); //数据行转实体
DataTable.To<List<Entity>>; //数据表转实体集合

//更复杂
DataTable.To<List<NameValueCollection>>(); 
List<Dictionary<string, object>>.To<DataTable>(); 
new { ID=1, Name="blqw"}.To<User>(); //匿名类转换

//变态嵌套
Dictionary<Guid, Dictionary<int, User>>
    .To<Dictionary<string, Dictionary<DateTime, NameValueCollection>>>(); //不能理解就算了

</details>

扩展自定义转换器

<details> <summary>示例代码,点击展开</summary>

[TestMethod]
public void 自定义转换器()
{
    var i = 11;
    var my = i.To<MyClass>();
    Assert.AreEqual(i, my.ID);
}

public class MyClass
{
    public int ID { get; set; }
}

public class MyConvertor : AllowNullConvertor<MyClass>, IFrom<int, MyClass>
{
    public ConvertResult<MyClass> From(IConvertContext context, int input)
    {
        return new MyClass() { ID = input };
    }
}

</details>

智能识别自定义转换方法

<details> <summary>示例代码,点击展开</summary>

[TestMethod]
public void 自定义强转()
{
    var i = 11;
    var my = i.To<MyClass>();
    Assert.AreEqual(i, my.ID);
}

public class MyClass
{
    public int ID { get; set; }
    public static explicit operator MyClass(int i) => new MyClass() { ID = i };
}
[TestMethod]
public void 自定义隐转()
{
    var i = 11;
    var my = i.To<MyClass2>();
    Assert.AreEqual(i, my.ID);
}

public class MyClass2
{
    public int ID { get; set; }
    public static implicit operator MyClass2(int i) => new MyClass2() { ID = i };
}

</details>

其他功能

<details> <summary>示例代码,点击展开</summary>

//数字转大写
Console.WriteLine(Converts.ToChineseAmount("123456456.789")); //壹亿贰仟叁佰肆拾伍万陆仟肆佰伍拾陆元柒角捌分
Console.WriteLine(Converts.ToChineseNumber("123456456.789")); //一亿二千三百四十五万六千四百五十六点七八九
Console.WriteLine(Converts.ToChineseAmount("123456456.789", true)); //一亿二千三百四十五万六千四百五十六元七角八分

//全半角转换
Console.WriteLine(Converts.ToDBC(",1234567aks"));//,1234567aks
Console.WriteLine(Converts.ToSBC("!1f23d.?@"));         //!1f23d.?@

//摘要/加密
Console.WriteLine(Converts.ToMD5("123456"));    //e10adc3949ba59abbe56e057f20f883e
Console.WriteLine(Converts.ToSHA1("123456"));   //7c4a8d09ca3762af61e59520943dc26494f8941b

//随机加密
var arr = new[]
{
    Converts.ToRandomMD5("123456"),
    Converts.ToRandomMD5("123456"),
    Converts.ToRandomMD5("123456"),
    Converts.ToRandomMD5("123456"),
    Converts.ToRandomMD5("123456"),
};

foreach (var g in arr)
{
    Console.WriteLine($"{g} : {Converts.EqualsRandomMD5("123456", g)}");
}
/*
fa91eefc-e903-dbcf-394b-0b757987357b : True
27abd3e0-fe0e-2eeb-1ff7-a60b03876465 : True
6d911bf2-0c59-0e01-5e87-7527dd1ee699 : True
0af7905a-0b3b-4eb4-b82b-0340f3438924 : True
1e024253-6bb9-fb25-4b67-3e42c265af02 : True
*/

</details>

JSON扩展

"{\"id\":\"name\":\"blqw\"}".To<User>();

Dynamic扩展

var a = new { id = 1, name = "zijian666" };
var b = a.ToDynamic();
Assert.IsTrue((int)b.ID == a.id);
Assert.IsTrue((string)b.NaMe == a.name);

Mapping扩展

private class MyClass1
{
    public string NickName { get; set; }
}

private class MyClass2
{
    public string Name { get; set; }
}

ConvertsMapping.Map<MyClass1, MyClass2>((m1, m2) => m2.Name = m1.NickName);
var my = new MyClass1
{
    Id = 1,
    NickName = "zijian666",
};
var my2 = my.To<MyClass2>();
Assert.IsNotNull(my2);
Assert.AreEqual(my.NickName, my2.Name);

老项目已停止维护

老项目已经停止维护 Convert3

Product 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 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 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 (5)

Showing the top 5 NuGet packages that depend on zijian666.Converts:

Package Downloads
zijian666.Converts.Json

Converts 超级转换器拓展 Json支持

zijian666.Converts.Dynamic

Converts 超级转换器拓展 动态类型(dynamic)支持

zijian666.HttpClientExtensions.Convert

为 zijian666.HttpClientExtensions 扩展 json和动态类型支持

zijian666.WebApiExtensions

用于快速创建简单易用的标准化WebApi项目

zijian666.Converts.Mapping

Converts 超级转换器拓展 支持按类型映射关系处理转换逻辑

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
5.3.2.1 138 6/17/2024
5.3.2 109 5/17/2024
5.3.1.1 313 3/4/2024
5.3.1 477 3/1/2024
5.3.0 548 1/25/2024
5.2.3 2,083 7/11/2023
5.2.0 1,628 7/1/2023
5.1.4 945 12/13/2022
5.1.3 773 12/8/2022
5.1.2 2,147 11/12/2022 5.1.2 is deprecated because it has critical bugs.
5.1.1 2,113 11/11/2022
5.0.1 3,206 11/2/2022