Masuit.Tools 2.2.4

一系列的常用的操作类扩展,大都是静态类,加密解密,反射操作,动态编译,硬件信息,字符串扩展方法,日期时间扩展操作,大文件拷贝,图像裁剪,验证码,断点续传,实体映射等常用封装。

There is a newer version of this package available.
See the version list below for details.
Install-Package Masuit.Tools -Version 2.2.4
dotnet add package Masuit.Tools --version 2.2.4
<PackageReference Include="Masuit.Tools" Version="2.2.4">
  <PrivateAssets>all</PrivateAssets>
  <IncludeAssets>runtime; build; native; contentfiles; analyzers</IncludeAssets>
</PackageReference>
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add Masuit.Tools --version 2.2.4
The NuGet Team does not provide support for this client. Please contact its maintainers for support.

表达式树对象映射支持配置化操作
在使用前需要像automapper那样,对mapper进行初始化操作

using Masuit.Tools.Mapping;
ExpressionMapper.CreateMap<ClassA, ClassADto>();// 默认关系映射
ExpressionMapper.CreateMap<ClassB, ClassBDto>().ForMember(s => s.ClassC.PropertyName, d => d.CustomName, true);// 自定义关系映射

ExpressionMapper.ConstructServicesUsing((x) => DependencyResolver.Current.GetService(x));// 使用依赖注入容器进行构造映射
//ExpressionMapper.ConstructServicesUsing((x) => ServiceLocator.Current.GetInstance(x));// 使用依赖注入容器进行构造映射
ExpressionMapper.CreateMap<Product, IProduct>().ConstructUsingServiceLocator().ReverseMap();// 链式自定义关系映射和反向映射

测试class:

public class TestClassA
{
    public string MyProperty { get; set; }
    public int Int { get; set; }
    public double Double { get; set; }
    public DateTime DateTime { get; set; }
    public TestClassC TestClassC { get; set; }
    public List<TestClassC> List { get; set; }
}

public class TestClassB
{
    public string MyProperty { get; set; }
    public int Int { get; set; }
    public double Double { get; set; }
    public DateTime DateTime { get; set; }
    public TestClassC TestClassC { get; set; }
    public List<TestClassD> List { get; set; }
}

public class TestClassC
{
    public string MyProperty { get; set; }
    public int Int { get; set; }
    public double Double { get; set; }
    public DateTime DateTime { get; set; }
    public TestClassD Obj { get; set; }
}

public class TestClassD
{
    public string MyProperty { get; set; }
    public int Int { get; set; }
    public double Double { get; set; }
    public DateTime DateTime { get; set; }
    public TestClassC Obj { get; set; }
}

构造一个结构相对复杂的对象:

var a = new TestClassA()
{
    MyProperty = "ssssssssssssssssssssss",
    DateTime = DateTime.Now,
    Double = 123.33,
    Int = 100,
    TestClassC = new TestClassC()
    {
        MyProperty = "ccccccccccccccccccccccccccc",
        DateTime = DateTime.Now,
        Double = 2345.555,
        Int = 10100,
        Obj = new TestClassD()
        {
            MyProperty = "ddddddddddddddddddddddddd",
            Obj = new TestClassC()
            {
                MyProperty = "cccccc",
                DateTime = DateTime.Now,
                Double = 23458894.555,
                Int = 10100000,
                Obj = new TestClassD()
            }
        }
    },
    List = new List<TestClassC>()
    {
        new TestClassC()
        {
            MyProperty = "cccccc",
            DateTime = DateTime.Now,
            Double = 2345.555,
            Int = 10100,
            Obj = new TestClassD()
            {
                MyProperty = "ddddddddddddddddddddddddddddddddddd",
                DateTime = DateTime.Now,
                Double = 2345.555,
                Int = 10100,
                Obj = new TestClassC()
                {
                    MyProperty = "cccccccccccccccccccccccccccccc",
                    DateTime = DateTime.Now,
                    Double = 2345.555,
                    Int = 10100,
                    Obj = new TestClassD()
                }
            }
        },
        new TestClassC()
        {
            MyProperty = "cccccc",
            DateTime = DateTime.Now,
            Double = 2345.555,
            Int = 10100,
            Obj = new TestClassD()
            {
                MyProperty = "ddddddddddddddddddddddddddddddddddd",
                DateTime = DateTime.Now,
                Double = 2345.555,
                Int = 10100,
                Obj = new TestClassC()
                {
                    MyProperty = "cccccccccccccccccccccccccccccc",
                    DateTime = DateTime.Now,
                    Double = 2345.555,
                    Int = 10100,
                    Obj = new TestClassD()
                }
            }
        },
        new TestClassC()
        {
            MyProperty = "cccccc",
            DateTime = DateTime.Now,
            Double = 2345.555,
            Int = 10100,
            Obj = new TestClassD()
            {
                MyProperty = "ddddddddddddddddddddddddddddddddddd",
                DateTime = DateTime.Now,
                Double = 2345.555,
                Int = 10100,
                Obj = new TestClassC()
                {
                    MyProperty = "cccccccccccccccccccccccccccccc",
                    DateTime = DateTime.Now,
                    Double = 2345.555,
                    Int = 10100,
                    Obj = new TestClassD()
                }
            }
        },
        new TestClassC()
        {
            MyProperty = "cccccc",
            DateTime = DateTime.Now,
            Double = 2345.555,
            Int = 10100,
            Obj = new TestClassD()
            {
                MyProperty = "ddddddddddddddddddddddddddddddddddd",
                DateTime = DateTime.Now,
                Double = 2345.555,
                Int = 10100,
                Obj = new TestClassC()
                {
                    MyProperty = "cccccccccccccccccccccccccccccc",
                    DateTime = DateTime.Now,
                    Double = 2345.555,
                    Int = 10100,
                    Obj = new TestClassD()
                }
            }
        },
        new TestClassC()
        {
            MyProperty = "cccccc",
            DateTime = DateTime.Now,
            Double = 2345.555,
            Int = 10100,
            Obj = new TestClassD()
            {
                MyProperty = "ddddddddddddddddddddddddddddddddddd",
                DateTime = DateTime.Now,
                Double = 2345.555,
                Int = 10100,
                Obj = new TestClassC()
                {
                    MyProperty = "cccccccccccccccccccccccccccccc",
                    DateTime = DateTime.Now,
                    Double = 2345.555,
                    Int = 10100,
                    Obj = new TestClassD()
                }
            }
        },
    }
};
var b = a.Map<TestClassA, TestClassB>();

性能测试:i7-4700H+12GB DDR3

#region 配置automapper

Mapper.Initialize(e =>
{
    e.CreateMap<TestClassA, TestClassB>().ReverseMap();
    e.CreateMap<TestClassC, TestClassD>().ReverseMap();
});

#endregion

#region 配置ExpressionMapper

ExpressionMapper.CreateMap<TestClassA, TestClassB>().ReverseMap();
ExpressionMapper.CreateMap<TestClassC, TestClassD>().ReverseMap();

#endregion

var time = HiPerfTimer.Execute(() =>
{
    a.Map<TestClassA, TestClassB>();
    a.Map<TestClassA, TestClassB>();
});// 因为第一次需要编译表达式树,所以测试两次
Console.WriteLine($"ExpressionMapper映射2次耗时:{time}s");// 0.0270508s
time = HiPerfTimer.Execute(() =>
{
    for (int i = 0; i < 1000000; i++)
    {
        var b = a.Map<TestClassA, TestClassB>();
    }
});
Console.WriteLine($"ExpressionMapper映射1000000次耗时:{time}s");// 1.206569s

time = HiPerfTimer.Execute(() =>
{
    Mapper.Map<TestClassB>(a);
    Mapper.Map<TestClassB>(a);
});// 映射2次为了和ExpressionMapper保持相同情况
Console.WriteLine($"AutoMapper映射2次耗时:{time}s");// 0.0281503s
time = HiPerfTimer.Execute(() =>
{
    for (int i = 0; i < 1000000; i++)
    {
        var b = Mapper.Map<TestClassB>(a);
    }
});
Console.WriteLine($"AutoMapper映射1000000次耗时:{time}s");// 4.1858825s

表达式树对象映射支持配置化操作
在使用前需要像automapper那样,对mapper进行初始化操作

using Masuit.Tools.Mapping;
ExpressionMapper.CreateMap<ClassA, ClassADto>();// 默认关系映射
ExpressionMapper.CreateMap<ClassB, ClassBDto>().ForMember(s => s.ClassC.PropertyName, d => d.CustomName, true);// 自定义关系映射

ExpressionMapper.ConstructServicesUsing((x) => DependencyResolver.Current.GetService(x));// 使用依赖注入容器进行构造映射
//ExpressionMapper.ConstructServicesUsing((x) => ServiceLocator.Current.GetInstance(x));// 使用依赖注入容器进行构造映射
ExpressionMapper.CreateMap<Product, IProduct>().ConstructUsingServiceLocator().ReverseMap();// 链式自定义关系映射和反向映射

测试class:

public class TestClassA
{
    public string MyProperty { get; set; }
    public int Int { get; set; }
    public double Double { get; set; }
    public DateTime DateTime { get; set; }
    public TestClassC TestClassC { get; set; }
    public List<TestClassC> List { get; set; }
}

public class TestClassB
{
    public string MyProperty { get; set; }
    public int Int { get; set; }
    public double Double { get; set; }
    public DateTime DateTime { get; set; }
    public TestClassC TestClassC { get; set; }
    public List<TestClassD> List { get; set; }
}

public class TestClassC
{
    public string MyProperty { get; set; }
    public int Int { get; set; }
    public double Double { get; set; }
    public DateTime DateTime { get; set; }
    public TestClassD Obj { get; set; }
}

public class TestClassD
{
    public string MyProperty { get; set; }
    public int Int { get; set; }
    public double Double { get; set; }
    public DateTime DateTime { get; set; }
    public TestClassC Obj { get; set; }
}

构造一个结构相对复杂的对象:

var a = new TestClassA()
{
    MyProperty = "ssssssssssssssssssssss",
    DateTime = DateTime.Now,
    Double = 123.33,
    Int = 100,
    TestClassC = new TestClassC()
    {
        MyProperty = "ccccccccccccccccccccccccccc",
        DateTime = DateTime.Now,
        Double = 2345.555,
        Int = 10100,
        Obj = new TestClassD()
        {
            MyProperty = "ddddddddddddddddddddddddd",
            Obj = new TestClassC()
            {
                MyProperty = "cccccc",
                DateTime = DateTime.Now,
                Double = 23458894.555,
                Int = 10100000,
                Obj = new TestClassD()
            }
        }
    },
    List = new List<TestClassC>()
    {
        new TestClassC()
        {
            MyProperty = "cccccc",
            DateTime = DateTime.Now,
            Double = 2345.555,
            Int = 10100,
            Obj = new TestClassD()
            {
                MyProperty = "ddddddddddddddddddddddddddddddddddd",
                DateTime = DateTime.Now,
                Double = 2345.555,
                Int = 10100,
                Obj = new TestClassC()
                {
                    MyProperty = "cccccccccccccccccccccccccccccc",
                    DateTime = DateTime.Now,
                    Double = 2345.555,
                    Int = 10100,
                    Obj = new TestClassD()
                }
            }
        },
        new TestClassC()
        {
            MyProperty = "cccccc",
            DateTime = DateTime.Now,
            Double = 2345.555,
            Int = 10100,
            Obj = new TestClassD()
            {
                MyProperty = "ddddddddddddddddddddddddddddddddddd",
                DateTime = DateTime.Now,
                Double = 2345.555,
                Int = 10100,
                Obj = new TestClassC()
                {
                    MyProperty = "cccccccccccccccccccccccccccccc",
                    DateTime = DateTime.Now,
                    Double = 2345.555,
                    Int = 10100,
                    Obj = new TestClassD()
                }
            }
        },
        new TestClassC()
        {
            MyProperty = "cccccc",
            DateTime = DateTime.Now,
            Double = 2345.555,
            Int = 10100,
            Obj = new TestClassD()
            {
                MyProperty = "ddddddddddddddddddddddddddddddddddd",
                DateTime = DateTime.Now,
                Double = 2345.555,
                Int = 10100,
                Obj = new TestClassC()
                {
                    MyProperty = "cccccccccccccccccccccccccccccc",
                    DateTime = DateTime.Now,
                    Double = 2345.555,
                    Int = 10100,
                    Obj = new TestClassD()
                }
            }
        },
        new TestClassC()
        {
            MyProperty = "cccccc",
            DateTime = DateTime.Now,
            Double = 2345.555,
            Int = 10100,
            Obj = new TestClassD()
            {
                MyProperty = "ddddddddddddddddddddddddddddddddddd",
                DateTime = DateTime.Now,
                Double = 2345.555,
                Int = 10100,
                Obj = new TestClassC()
                {
                    MyProperty = "cccccccccccccccccccccccccccccc",
                    DateTime = DateTime.Now,
                    Double = 2345.555,
                    Int = 10100,
                    Obj = new TestClassD()
                }
            }
        },
        new TestClassC()
        {
            MyProperty = "cccccc",
            DateTime = DateTime.Now,
            Double = 2345.555,
            Int = 10100,
            Obj = new TestClassD()
            {
                MyProperty = "ddddddddddddddddddddddddddddddddddd",
                DateTime = DateTime.Now,
                Double = 2345.555,
                Int = 10100,
                Obj = new TestClassC()
                {
                    MyProperty = "cccccccccccccccccccccccccccccc",
                    DateTime = DateTime.Now,
                    Double = 2345.555,
                    Int = 10100,
                    Obj = new TestClassD()
                }
            }
        },
    }
};
var b = a.Map<TestClassA, TestClassB>();

性能测试:i7-4700H+12GB DDR3

#region 配置automapper

Mapper.Initialize(e =>
{
    e.CreateMap<TestClassA, TestClassB>().ReverseMap();
    e.CreateMap<TestClassC, TestClassD>().ReverseMap();
});

#endregion

#region 配置ExpressionMapper

ExpressionMapper.CreateMap<TestClassA, TestClassB>().ReverseMap();
ExpressionMapper.CreateMap<TestClassC, TestClassD>().ReverseMap();

#endregion

var time = HiPerfTimer.Execute(() =>
{
    a.Map<TestClassA, TestClassB>();
    a.Map<TestClassA, TestClassB>();
});// 因为第一次需要编译表达式树,所以测试两次
Console.WriteLine($"ExpressionMapper映射2次耗时:{time}s");// 0.0270508s
time = HiPerfTimer.Execute(() =>
{
    for (int i = 0; i < 1000000; i++)
    {
        var b = a.Map<TestClassA, TestClassB>();
    }
});
Console.WriteLine($"ExpressionMapper映射1000000次耗时:{time}s");// 1.206569s

time = HiPerfTimer.Execute(() =>
{
    Mapper.Map<TestClassB>(a);
    Mapper.Map<TestClassB>(a);
});// 映射2次为了和ExpressionMapper保持相同情况
Console.WriteLine($"AutoMapper映射2次耗时:{time}s");// 0.0281503s
time = HiPerfTimer.Execute(() =>
{
    for (int i = 0; i < 1000000; i++)
    {
        var b = Mapper.Map<TestClassB>(a);
    }
});
Console.WriteLine($"AutoMapper映射1000000次耗时:{time}s");// 4.1858825s

Release Notes

如有问题请联系QQ:3444764617,或者到项目的github反馈问题,详细的API文档在github上:https://github.com/ldqk/Masuit.Tools

This package is not used by any popular GitHub repositories.

Version History

Version Downloads Last updated
2.2.7.3 51 10/30/2019
2.2.7.1 39 10/29/2019
2.2.6.4 48 10/16/2019
2.2.6.2 117 6/9/2019
2.2.6 132 4/9/2019
2.2.5.1 104 3/28/2019
2.2.5 111 3/20/2019
2.2.4.2 164 2/10/2019
2.2.4.1 123 1/24/2019
2.2.4 152 1/19/2019
2.2.3.3 188 1/6/2019
2.2.3 164 1/2/2019
2.2.2 154 12/31/2018
2.2.0.1 142 12/29/2018
2.2.0 144 12/29/2018
2.1.6.1 114 12/25/2018
2.1.5.1 140 12/22/2018
2.1.4.3 110 12/15/2018
2.1.4.1 123 12/15/2018
2.1.4 145 11/27/2018
2.1.3.1 134 11/23/2018
2.1.1 209 9/22/2018
2.1.0 165 9/6/2018
2.0.0 216 7/27/2018
1.9.11 238 7/10/2018
1.9.10 272 6/27/2018
1.9.9 243 6/6/2018
1.9.8 280 5/9/2018
1.9.7.6 401 4/21/2018
1.9.7.5 368 4/21/2018
1.9.7.4 371 4/21/2018
1.9.7.3 290 4/17/2018
1.9.7.2 295 4/16/2018
1.9.7.1 269 4/16/2018
1.9.7 294 4/16/2018
1.9.6 377 4/14/2018
1.9.5.4 335 4/2/2018
1.9.5.3 262 4/2/2018
1.9.5.2 441 4/1/2018
1.9.5.1 398 3/31/2018
1.9.5 420 3/25/2018
1.9.4.1 354 3/17/2018
1.9.4 248 3/17/2018
1.9.3.3 263 3/14/2018
1.9.3.2 237 3/13/2018
1.9.3.1 280 3/11/2018
1.9.3 274 3/11/2018
1.9.2 285 3/11/2018
1.9.1 281 3/9/2018
1.9.0 279 3/8/2018
1.8.7 305 3/3/2018
1.8.6 293 2/20/2018
1.8.5 305 2/3/2018
1.8.4 303 1/14/2018
1.8.3 300 1/13/2018
1.8.2 342 1/10/2018
1.8.1 382 1/5/2018
1.8.0 281 12/22/2017
1.7.9 287 12/8/2017
1.7.8 492 11/18/2017
1.7.7 236 11/7/2017
1.7.6 279 10/5/2017
1.7.5 249 10/1/2017
1.7.4 256 9/25/2017
1.7.2 240 9/22/2017
1.7.1 236 9/22/2017
1.7.0 235 9/22/2017
1.6.9 253 9/21/2017
1.6.8 259 9/15/2017
1.6.7 241 9/13/2017
1.6.6 260 9/10/2017
1.6.5 239 9/10/2017
1.6.4 232 9/8/2017
1.6.2 250 9/8/2017
1.6.1 248 9/7/2017
1.6.0 252 9/7/2017
1.5.1 245 8/31/2017
1.4.5 251 8/26/2017
1.4.4 272 8/25/2017
1.4.3 248 8/24/2017
1.4.2 272 8/7/2017
1.3.0.3 259 8/4/2017
1.3.0.2 261 8/4/2017
1.3.0.1 259 8/4/2017
1.3.0 262 7/27/2017
1.2.9 280 7/24/2017
1.2.8 268 7/23/2017
1.2.7 271 7/19/2017
1.2.6 259 7/12/2017
1.2.5 262 7/9/2017
1.2.4 353 7/8/2017
1.2.3 388 5/4/2017
1.2.1 335 4/26/2017
1.1.2 353 4/24/2017
1.1.0 361 4/10/2017
1.0.9 391 3/22/2017
1.0.8.2 334 3/8/2017
1.0.6 312 3/6/2017
1.0.5 332 3/5/2017
1.0.4 331 3/2/2017
1.0.2 343 2/19/2017
1.0.1 334 2/18/2017
Show less