An open API service indexing awesome lists of open source software.

https://github.com/adamfoneil/copyobject

A small library for copying plain objects with identically-named properties
https://github.com/adamfoneil/copyobject

object-mapping

Last synced: 8 months ago
JSON representation

A small library for copying plain objects with identically-named properties

Awesome Lists containing this project

README

          

[![Build status](https://ci.appveyor.com/api/projects/status/43ykvn36cq4u67mu?svg=true)](https://ci.appveyor.com/project/adamosoftware/copyobject)
[![Nuget](https://img.shields.io/nuget/v/AO.CopyObject)](https://www.nuget.org/packages/AO.CopyObject/)

The idea here is to copy one object to another or to a new instance, typically from some kind of DTO to a database model class or vice versa via extension method. For example

```csharp
using CopyObjectLibrary;

...

var dest = source.CopyAs();
```
This would copy matching properties of `source` to a new instance of type `SomeType dest`.

This mapper requires properties to be identically-named and a type that's [close enough](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/TypeExtensions.cs#L12). "Close enough" means `T` == `T` but also `T` == `Nullable` or `Nullable` == `T`.

See examples in [tests](https://github.com/adamfoneil/CopyObject/blob/master/Testing/CopyTests.cs).

# Reference
## CopyObjectLibrary.ObjectExtensions [ObjectExtensions.cs](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/ObjectExtensions.cs#L7)
### Methods
- [T](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/ObjectExtensions.cs#L14) [CopyAs](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/ObjectExtensions.cs#L14)
(this object object)
- [T](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/ObjectExtensions.cs#L20) [CopyAs](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/ObjectExtensions.cs#L20)
(this object object, T instance)
- void [CopyTo](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/ObjectExtensions.cs#L39)
(this object object, T instance)
- bool [Equals](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/ObjectExtensions.cs#L43)
(PropertyInfo x, PropertyInfo y)
- int [GetHashCode](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/ObjectExtensions.cs#L46)
(PropertyInfo obj)

## CopyObjectLibrary.TypeExtensions [TypeExtensions.cs](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/TypeExtensions.cs#L6)
### Methods
- bool [IsSimpleType](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/TypeExtensions.cs#L8)
(this Type type)
- bool [IsNullableGeneric](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/TypeExtensions.cs#L10)
(this Type type)
- bool [IsTypeCloseEnough](https://github.com/adamfoneil/CopyObject/blob/master/CopyObject/TypeExtensions.cs#L12)
(this Type type, Type compareWith)