-
Notifications
You must be signed in to change notification settings - Fork 391
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: |
|
Hey @DocSvartz, It would be way easier to follow your PR and give you better Feedback when needed, if you would have included some more information 😅 **GitHub Issue:** closes #
<!-- Link to relevant GitHub issue if applicable. All PRs should be associated with an issue, unless the change is documentation related. -->
## PR Type:
<!--
Copy the labels that apply to this PR and paste them above:
- 🐞 Bugfix
- ✨ Feature
- 🎨 Code style update (formatting)
- 🔄 Refactoring (no functional changes, no api changes)
- 🏗️ Build or CI related changes
- 📚 Documentation content changes
- 🤖 Project automation
- 💬 Other... (Please describe)
-->
## What is the current behavior? 🤔
<!-- Please describe the current behavior that you are modifying, or link to a relevant issue. -->
## What is the new behavior? 🚀
<!-- Please describe the new behavior after your modifications. -->
## PR Checklist ✅
Please check if your PR fulfills the following requirements:
- [ ] 📝 Commits must be following the [Conventional Commits](https://www.conventionalcommits.org/en/v1.0.0/#summary) specification.
- [ ] 🧪 Added [Runtime tests, UI tests, or a manual test sample](https://github.com/**/blob/master/docs/articles/contributing/*.md) for the changes have been added (for bug fixes / features) (if applicable)
- [ ] 📚 Docs have been added/updated which fit [documentation template](https://github.com/**/blob/master/docs/.feature-template.md) (for bug fixes / features)
- [ ] 🖼️ Validated PR `Screenshots Compare Test Run` results.
- [ ] ❗ Contains **NO** breaking changes
<!-- If this PR contains a breaking change, please describe the impact and migration path for existing applications below.
Please note that breaking changes are likely to be rejected -->
## Other information ℹ️
<!-- Please provide any additional information if necessary --> |
Need Help :