Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ethern-myth/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.
https://github.com/ethern-myth/objectspread
arrays c-sharp dotnet net8
Last synced: 20 days ago
JSON representation
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.
- Host: GitHub
- URL: https://github.com/ethern-myth/objectspread
- Owner: Ethern-Myth
- License: mit
- Created: 2024-05-15T17:32:11.000Z (7 months ago)
- Default Branch: main
- Last Pushed: 2024-05-17T10:32:48.000Z (7 months ago)
- Last Synced: 2024-12-02T09:05:51.852Z (21 days ago)
- Topics: arrays, c-sharp, dotnet, net8
- Language: C#
- Homepage:
- Size: 11.7 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 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.
![NuGet Version](https://img.shields.io/nuget/v/objectSpread)
![NuGet Downloads](https://img.shields.io/nuget/dt/objectSpread)
## 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:
```bash
Install-Package objectSpread
```Or via .NET CLI:
```bash
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:
```csharp
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(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(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](https://github.com/Ethern-Myth).
## License
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.
---
For more details and updates, please visit the [GitHub repository](https://github.com/Ethern-Myth/objectSpread).