New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[API Proposal]: Bulk copy properties from one object to another #101676
Comments
Tagging subscribers to this area: @dotnet/area-system-reflection |
This is a proposal for a reflection heavy API. It is not appropriate to be on
If you do not want to type this pattern out for every property, it sounds like a job for a source generator. |
I assume a de-facto standard tool for this is https://github.com/AutoMapper/AutoMapper, I guess there are SG-based options too |
It Operates on a lower level as it Bulk copies field values on raw Level. Since the types are equal (of both objects) we can operate on a lower Level. Also the bulk copying might be faster than writing the assignment Statements by Hand |
But the pattern is solved already with source generators.
Why is that important here. The motivation was listed as the inconvenience of manually writing it. There's no motivation provided for needing something "lower level". |
This was in reference to the complaint about the method being in |
Instead, is there an API to copy all fields to another object? Like |
There currently seems to not be any approach. MemberwiseClone, as you already said, creates a new instance. My approach is a Copy From->To approach which uses existing references. |
Are you suggesting a bitwise copy? |
And my source generator question still applies here. |
Closing; several edge cases and concerns:
|
Background and motivation
Problem
The
pattern is very inconvenient to write out if the purpose is just to Update all properties of one object to the properties of another one (same type).
Also this approach is not the fastest possible if we consider raw byte bulk copying fields from one object to another (assuming same type)
Where does this pattern occur?
One potential Use-Case: Updating an Entity by using Entity Framework. I do not want to type this pattern out for every property:
Another one would be: you keep track of objects in-memory and you provide an Update API. The User can pass an object with the same type to the method and you want to update all properties.
More explicit example using ASP:
In this example I may not be able just to replace the entire object since the object could be referenced multiple times by other things. The old reference would not be available anymore.
API Proposal
We could instead introduce
Unsafe.CopyProperties<T>(from: t1, to: t2)
which would basically bulk copy all field values from obj1 to obj2 (thus the properties are changed since they just get and set the backing fields). Since obj1 and obj2 are equal in type, we can even assume its safe to copy raw bytes over to the other object.This would make it a lot more convenient as you do not need to type out every property. And it would be faster than the reflection approach.
API Usage
It would look like this:
Alternative Designs
No response
Risks
Raw byte bulk-copy may be possible or not, that depends on the internals which needs to be discussed.
The text was updated successfully, but these errors were encountered: