TypeMerger 2.0.1

There is a newer version of this package available.
See the version list below for details.
dotnet add package TypeMerger --version 2.0.1                
NuGet\Install-Package TypeMerger -Version 2.0.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="TypeMerger" Version="2.0.1" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add TypeMerger --version 2.0.1                
#r "nuget: TypeMerger, 2.0.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 TypeMerger as a Cake Addin
#addin nuget:?package=TypeMerger&version=2.0.1

// Install TypeMerger as a Cake Tool
#tool nuget:?package=TypeMerger&version=2.0.1                

TypeMerger - Merge two objects into one

TypeMerger is a simple merging utility class that will take two objects of any type and merge them into a single object of a new type. This new Type is a dynamically generated and loaded using System.Reflection.Emit.

How is it used?

Simple usage

This will result in a new object that has All the properties from obj1 and obj2.

var result = TypeMerger.Merge(obj1, obj2);

Ignore Certain Properties

This will result in a new object that has all of the properties from Obj1 and Obj2 Except for .SomeProperty from obj1 and .AnotherProperty from obj2.

var result = TypeMerger.Ignore(() => obj1.SomeProperty)
                       .Ignore(() => obj2.AnotherProperty)
                       .Merge(obj1, obj2); 

What About Collisions?

If both objects have the same property there is a fluent method that will tell the Merger which object to use for that property. You simply tell the Merger which property to Use.

In this example given obj1 and obj2 that both have .SomeProperty, the value from obj2 will be used.

var result = TypeMerger.Use(() => obj2.SomeProperty)
                       .Merge(obj1, obj2);

Mix & Match Your Merge

Combining the .Ignore and .Use fluent methods allows you to pick and choose what you want from your objects.

var obj1 = new {
    SomeProperty = "foo",
    SomeAmount = 20,
    AnotherProperty = "yo"
};

var obj2 = new {
    SomeProperty = "bar",
    SomePrivateStuff = "SECRET!!",
    SomeOtherProperty = "more stuff"
};

var result = TypeMerger.Ignore(() => obj1.AnotherProperty)
                       .Use(() => obj2.SomeProperty)
                       .Ignore(() => obj2.SomePrivateStuff)
                       .Merge(obj1, obj2);

The result object will have the following properties and values:

SomeProperty: "bar"
SomeAmount: 20
SomeOtherProperty: "more stuff"

History

The code is based on the original TypeMerger class written by Mark Miller. Updated, enhanced, and now maintained by Kyle Finley.

Original posting: KyleFinley.net/typemerger

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 netcoreapp2.2 is compatible.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETCoreApp 2.2

    • No dependencies.

NuGet packages (3)

Showing the top 3 NuGet packages that depend on TypeMerger:

Package Downloads
OuroborosAI.Core

Powerful layer on top of OpenAI supporting chaining and recursion scenarios. Includes fluent SDK for chaining, templates, and text processing. Still very early an .NET 7 only; Docs and broader support to follow.

AspNetCore.InertiaCore

Inertia.js ASP.NET Adapter. https://inertiajs.com/

Spark.InertiaJs

Inertia.js ASP.NET Adapter. https://inertiajs.com/

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
2.1.4 177,431 6/26/2023
2.1.3 9,212 5/30/2023
2.1.2 17,141 2/11/2023
2.1.1 93,765 3/17/2021
2.1.0 21,280 9/30/2019
2.0.1 7,561 1/30/2019
2.0.0 34,996 1/29/2019