ObjectUtils 1.2.3.4

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

// Install ObjectUtils as a Cake Tool
#tool nuget:?package=ObjectUtils&version=1.2.3.4                

ObjectUtils

介绍

ObjectUtils包含一组工具,用于提高编程效率。目前包含的工具有:

工具 简介 完成情况
ObjectMapper 对象映射工具,使用表达式树实现,支持映射配置,采用深克隆模式。效率略逊于TinyMapper,优点是不用先Bind需要映射的类型 已完成
ObjectComparator 对象比较工具,使用表达式树实现,支持映射配置,会忽略没有匹配的字段 已完成
软件架构

目录结构说明:

目录 说明 公开类型/接口
Comparator ObjectComparator实现 ObjectComparator
Config 对象映射配置 IMapConfig<TSource, TTarget>:配置接口 <br/>ObjectMapConfigurator:配置入口
Core 类型定义、管理类 --
Extensions 扩展方法 ObjectMapperExtensions <br/>ObjectComparatorExtensions
Mapper ObjectMapper实现 ObjectMapper
Util 工具类 --
安装教程

引用本项目或生成的Dll即可,是否上传到Nuget待定。

使用说明

使用方法可以参考TestProject中的Usage:

// 测试类定义
public class ClassA
{
    public string Name { get; set; }
    public int Age;
    public int Tall;
    public bool IgnoreField;
}

public class ClassB
{
    public string Title { get; set; }
    public long longAge;
    public int Tall;
    public string IgnoreField;
}

// 使用方法
public class Usage
{
    public Usage()
    {
        // 配置映射关系,使配置对ObjectMapper和ObjectComparator都生效
        ObjectMapConfigurator.Config<ClassA, ClassB>(config =>
        {
            // 不同名字段的映射
            // Tall字段名称相同,不用配置
            config.Map(s => s.Name, t => t.Title);
            // 这个规则适用于所有的字段
            config.MapRule(s => $"long{s}");
            // 忽略的字段或属性
            //config.IgnoreSource(s => s.IgnoreField);
            config.IgnoreTarget(t => t.IgnoreField);
        });
    }

    // ObjectMapper 示例
    [Fact]
    public void ObjectMapperUsage()
    {
        var a = new ClassA
        {
            Age = 30,
            IgnoreField = true,
            Name = "Jack",
            Tall = 180,
        };

        // 将a对象映射为b对象
        var b = ObjectMapper.Map<ClassA, ClassB>(a);
        // 或者使用扩展方法
        //var b = a.Map<ClassA, ClassB>();
        // 克隆对象,这是一个扩展方法
        var copied = b.Clone();

        Assert.NotNull(copied);
        Assert.Equal(a.Age, copied.longAge);
        Assert.Equal(a.Name, copied.Title);
        Assert.Equal(a.Tall, copied.Tall);
        Assert.Null(copied.IgnoreField);
    }

    // ObjectComparator 示例
    [Fact]
    public void ObjectComparatorUsage()
    {
        var a = new ClassA
        {
            Age = 30,
            IgnoreField = true,
            Name = "Jack",
            Tall = 180,
        };

        var b = new ClassB
        {
            longAge = 29,
            IgnoreField = false.ToString(),
            Title = "Jack",
            Tall = 178,
        };

        var result = ObjectComparator.Equals(a, b);
        //var result = a.AreEquals(b);
        
        Assert.False(result);
    }

    [Fact]
    public void ObjectComparatorUsage2()
    {
        var a = new ClassA
        {
            Age = 30,
            IgnoreField = true,
            Name = "Jack",
            Tall = 180,
        };

        var b = a.Map<ClassA, ClassB>();

        var result = ObjectComparator.Equals(a, b);
        //var result = a.AreEquals(b);

        Assert.True(result);
    }
}
参与贡献
  1. Fork 本仓库
  2. 新建 Feat_xxx 分支
  3. 提交代码
  4. 新建 Pull Request
参考

TinyMapper
C# 高性能对象映射(表达式树实现)

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 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. 
.NET Core netcoreapp1.0 was computed.  netcoreapp1.1 was computed.  netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard1.3 is compatible.  netstandard1.4 was computed.  netstandard1.5 was computed.  netstandard1.6 was computed.  netstandard2.0 was computed.  netstandard2.1 was computed. 
.NET Framework net40 is compatible.  net403 was computed.  net45 was computed.  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 tizen30 was computed.  tizen40 was computed.  tizen60 was computed. 
Universal Windows Platform uap was computed.  uap10.0 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
1.2.3.4 263 1/19/2022