objectSpread 2.5.0
dotnet add package objectSpread --version 2.5.0
NuGet\Install-Package objectSpread -Version 2.5.0
<PackageReference Include="objectSpread" Version="2.5.0" />
paket add objectSpread --version 2.5.0
#r "nuget: objectSpread, 2.5.0"
// Install objectSpread as a Cake Addin
#addin nuget:?package=objectSpread&version=2.5.0
// Install objectSpread as a Cake Tool
#tool nuget:?package=objectSpread&version=2.5.0
ObjectSpread
ObjectSpread
is a C# utility library that provides an efficient way to merge properties from multiple objects into a new object. It uses reflection and caching to enhance performance, making it a useful tool for scenarios where object merging is required.
Features
- Merge Multiple Objects: Combine properties from multiple source objects into a new instance of a target type.
- Reflection and Caching: Utilizes reflection to dynamically access properties and caching for improved performance.
- Type Safety: Ensures that properties are only merged if they are compatible and can be written to the target object.
Installation
To include ObjectSpread
in your project, you can add the source files directly or, if available, install via a package manager like NuGet.
NuGet
To install via NuGet, run the following command in the NuGet Package Manager Console:
Install-Package objectSpread
Or via .NET CLI:
dotnet add package objectSpread
Direct Download
Alternatively, you can download the source code and include it in your project manually.
Usage
Here's a basic example demonstrating how to use the Spread
method from the ObjectExtensions
class to merge properties from multiple objects:
using objectSpread;
class Program
{
static void Main(string[] args)
{
var obj1 = new { Name = "Alice", Age = 25 };
var obj2 = new { Age = 30, Country = "Wonderland" };
var merged = ObjectExtensions.Spread<Person>(obj1, obj2);
Console.WriteLine($"Name: {merged.Name}, Age: {merged.Age}, Country: {merged.Country}");
}
}
public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public string Country { get; set; }
}
Output
Name: Alice, Age: 30, Country: Wonderland
In this example, properties from obj1
and obj2
are merged into a new Person
object. The Age
property from obj2
overwrites the Age
property from obj1
.
API Reference
ObjectExtensions
Spread<T>(params object[] sources) where T : class, new()
Merges the properties of multiple source objects into a new instance of the target type T
.
- T: The type of the target object.
- sources: An array of source objects whose properties will be merged.
Returns: A new instance of type T
with merged properties.
Author
objectSpread is developed and maintained by Ethern Myth.
License
This project is licensed under the MIT License. See the LICENSE file for details.
For more details and updates, please visit the GitHub repository.
Product | Versions Compatible and additional computed target framework versions. |
---|---|
.NET | net8.0 is compatible. 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. |
-
net8.0
- No dependencies.
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 |
---|---|---|
2.5.0 | 90 | 5/15/2024 |