当前位置:   article > 正文

C#进阶-.NET Framework使用工具类简化对象之间的属性复制和操作_c# 对象属性复制工具类

c# 对象属性复制工具类

C#编程中,对象之间的属性复制和操作是一个常见的需求。为此,.NET Framework提供了多种实用工具库,如AutoMapper、ValueInjecter和ExpressMapper。这些库通过简化代码,提高了开发效率。本文将介绍这些工具库,比较它们的特点,提供使用示例,并总结它们的优缺点,帮助开发者选择合适的工具库。

在这里插入图片描述

一、适用于属性复制的实用工具库

1、工具库列举

在.NET Framework中,有几个常用的工具库可以用于简化对象之间的属性复制和操作。以下是一些常用的库:

工具库名称支持的.NET版本安装方式特点
AutoMapper.NET Framework 4.x, .NET Core, .NET StandardInstall-Package AutoMapper功能强大,社区支持广泛,配置灵活
ValueInjecter.NET Framework 4.x, .NET Core, .NET StandardInstall-Package Omu.ValueInjecter轻量级,简单易用
ExpressMapper.NET Framework 4.x, .NET Core, .NET StandardInstall-Package ExpressMapper高效快速,代码简洁

2、工具库对比

下面是对这些工具库的详细对比:

特性/工具库AutoMapperValueInjecterExpressMapper
安装复杂度中等简单简单
学习曲线中等
配置灵活性中等
性能中等
社区支持广泛一般一般
自定义映射支持支持支持
深度克隆支持支持不支持

二、各工具库使用代码示例

1、AutoMapper

AutoMapper 是一个功能强大的对象映射库,广泛用于企业项目中。下面是一些常用功能的代码示例。

安装

Install-Package AutoMapper
  • 1

使用示例

① 基本映射
using AutoMapper;

public class Source
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Destination
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Program
{
    public static void Main()
    {
        var config = new MapperConfiguration(cfg => cfg.CreateMap<Source, Destination>());
        var mapper = config.CreateMapper();

        var source = new Source { Name = "Alice", Age = 30 };
        var destination = mapper.Map<Destination>(source);

        Console.WriteLine($"Name: {destination.Name}, Age: {destination.Age}");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
② 自定义映射
using AutoMapper;

public class Source
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Destination
{
    public string FullName { get; set; }
}

public class Program
{
    public static void Main()
    {
        var config = new MapperConfiguration(cfg => 
        {
            cfg.CreateMap<Source, Destination>()
               .ForMember(dest => dest.FullName, opt => opt.MapFrom(src => src.FirstName + " " + src.LastName));
        });
        var mapper = config.CreateMapper();

        var source = new Source { FirstName = "John", LastName = "Doe" };
        var destination = mapper.Map<Destination>(source);

        Console.WriteLine($"FullName: {destination.FullName}");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

2、ValueInjecter

ValueInjecter 是一个轻量级的对象映射库,适用于简单的属性复制场景。

安装

Install-Package Omu.ValueInjecter
  • 1

使用示例

① 基本映射
using Omu.ValueInjecter;

public class Source
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Destination
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Program
{
    public static void Main()
    {
        var source = new Source { Name = "Alice", Age = 30 };
        var destination = new Destination();
        destination.InjectFrom(source);

        Console.WriteLine($"Name: {destination.Name}, Age: {destination.Age}");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
② 自定义映射
using Omu.ValueInjecter;

public class Source
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Destination
{
    public string FullName { get; set; }
}

public class CustomInjection : ConventionInjection
{
    protected override bool Match(ConventionInfo c)
    {
        return c.SourceProp.Name == "FirstName" && c.TargetProp.Name == "FullName";
    }

    protected override object SetValue(ConventionInfo c)
    {
        return ((string)c.SourceProp.Value) + " " + ((Source)c.Source).LastName;
    }
}

public class Program
{
    public static void Main()
    {
        var source = new Source { FirstName = "John", LastName = "Doe" };
        var destination = new Destination();
        destination.InjectFrom<CustomInjection>(source);

        Console.WriteLine($"FullName: {destination.FullName}");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37

3、ExpressMapper

ExpressMapper 是一个高效的对象映射库,适用于需要高性能映射的场景。

安装

Install-Package ExpressMapper
  • 1

使用示例

① 基本映射
using ExpressMapper;

public class Source
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Destination
{
    public string Name { get; set; }
    public int Age { get; set; }
}

public class Program
{
    public static void Main()
    {
        Mapper.Register<Source, Destination>();
        Mapper.Compile();

        var source = new Source { Name = "Alice", Age = 30 };
        var destination = Mapper.Map<Source, Destination>(source);

        Console.WriteLine($"Name: {destination.Name}, Age: {destination.Age}");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
② 自定义映射
using ExpressMapper;

public class Source
{
    public string FirstName { get; set; }
    public string LastName { get; set; }
}

public class Destination
{
    public string FullName { get; set; }
}

public class Program
{
    public static void Main()
    {
        Mapper.Register<Source, Destination>()
              .Member(dest => dest.FullName, src => src.FirstName + " " + src.LastName);
        Mapper.Compile();

        var source = new Source { FirstName = "John", LastName = "Doe" };
        var destination = Mapper.Map<Source, Destination>(source);

        Console.WriteLine($"FullName: {destination.FullName}");
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27

三、适用于属性复制的工具库优缺点对比

以下是 AutoMapper、ValueInjecter 和 ExpressMapper 的优缺点对比:

工具库名称优点缺点
AutoMapper功能强大,支持复杂的映射场景
配置灵活,支持自定义映射
社区支持广泛,文档丰富
学习曲线较陡,需要时间熟悉配置
在一些简单场景中可能显得过于复杂
ValueInjecter轻量级,使用简单
适用于简单的属性复制场景
功能相对有限,支持复杂映射的能力不强
社区支持和文档较少
ExpressMapper高效快速,性能优异
代码简洁,易于使用
功能相对较少,支持的映射场景有限
社区支持和文档较少
  • AutoMapper 是一个功能强大且配置灵活的工具,适合处理复杂的映射场景,但学习曲线较陡,适合需要高级映射功能的项目。
  • ValueInjecter 是一个轻量级、易于使用的工具,适合处理简单的属性复制场景,但功能有限,适合需要简单映射的项目。
  • ExpressMapper 是一个高效快速的工具,性能优异,适合对性能有较高要求的项目,但功能较少,适合需要简单高效映射的项目。

四、适用于属性复制的实用工具库总结

在选择适用于属性复制的工具库时,需要根据项目的具体需求进行选择。如果需要处理复杂的映射场景,推荐使用AutoMapper;如果仅需要处理简单的属性复制,可以选择ValueInjecter;如果对性能有较高要求,可以选择ExpressMapper。通过合理选择工具库,可以大大简化对象属性复制的代码,提高开发效率。

在这里插入图片描述

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/一键难忘520/article/detail/868475
推荐阅读
相关标签
  

闽ICP备14008679号