[Proposal]: Bulk copy properties from one object to another #8082
Replies: 4 comments 3 replies
-
Duplicates #162 ? |
Beta Was this translation helpful? Give feedback.
1 reply
-
@NETKroks This does not require a language change and should probably be posted here. |
Beta Was this translation helpful? Give feedback.
1 reply
-
The issue is discussed here: dotnet/runtime#101676 |
Beta Was this translation helpful? Give feedback.
0 replies
-
Sounds like a job for source generators. |
Beta Was this translation helpful? Give feedback.
1 reply
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
Uh oh!
There was an error while loading. Please reload this page.
-
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 reference would not be available anymore.
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.
With this the code above would look like:
Beta Was this translation helpful? Give feedback.
All reactions