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
- Host: GitHub
- URL: https://github.com/adamfoneil/copyobject
- Owner: adamfoneil
- License: mit
- Created: 2021-03-01T18:02:10.000Z (about 5 years ago)
- Default Branch: master
- Last Pushed: 2021-03-01T19:10:29.000Z (about 5 years ago)
- Last Synced: 2025-06-25T00:07:37.008Z (9 months ago)
- Topics: object-mapping
- Language: C#
- Homepage:
- Size: 15.6 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://ci.appveyor.com/project/adamosoftware/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)