-
Notifications
You must be signed in to change notification settings - Fork 390
Implementation of proposed Typeadapter concept from issue #832 #835
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
base: development
Are you sure you want to change the base?
Implementation of proposed Typeadapter concept from issue #832 #835
Conversation
Yes, but in fact in Mapster it works wit it as a mutable object. |
| /// <summary> | ||
| /// Adapt the source object to an existing destination object. | ||
| /// </summary> | ||
| /// <param name="source">Source object to adapt.</param> | ||
| /// <param name="destination">Destination object to populate.</param> | ||
| /// <param name="sourceType">The type of the source object.</param> | ||
| /// <param name="destinationType">The type of the destination object.</param> | ||
| /// <returns>Adapted destination type.</returns> | ||
| public static object? Adapt(this object source, object destination, Type sourceType, Type destinationType) | ||
| { | ||
| return Adapt(source, destination, sourceType, destinationType, TypeAdapterConfig.GlobalSettings); | ||
| } | ||
|
|
||
| /// <summary> | ||
| /// Adapt the source object to an existing destination object. | ||
| /// </summary> | ||
| /// <param name="source">Source object to adapt.</param> | ||
| /// <param name="destination">Destination object to populate.</param> | ||
| /// <param name="sourceType">The type of the source object.</param> | ||
| /// <param name="destinationType">The type of the destination object.</param> | ||
| /// <param name="config">Configuration</param> | ||
| /// <returns>Adapted destination type.</returns> | ||
| public static object? Adapt(this object source, object destination, Type sourceType, Type destinationType, TypeAdapterConfig config) | ||
| { | ||
| var del = config.GetMapToTargetFunction(sourceType, destinationType); | ||
| if (sourceType.GetTypeInfo().IsVisible && destinationType.GetTypeInfo().IsVisible) | ||
| { | ||
| dynamic fn = del; | ||
| return fn((dynamic)source, (dynamic)destination); | ||
| } | ||
| else | ||
| { | ||
| //NOTE: if type is non-public, we cannot use dynamic | ||
| //DynamicInvoke is slow, but works with non-public | ||
| return del.DynamicInvoke(source, destination); | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't remember exactly if this is the case here.
But in fact, generic and non-generic adapters behave differently. 😂
There's another problem. There are settings that make any type seem "immutable". .ConstructUsing() Maybe: |
Need Help :