Skip to content

Conversation

@DocSvartz
Copy link

@DocSvartz DocSvartz commented Dec 12, 2025

Need Help :

  • Is the struct immutable ?
  • Are there any other types that would be considered immutable?
  • Help with adapter name
  • Help with description of adapter

@DocSvartz DocSvartz marked this pull request as draft December 12, 2025 11:53
@DocSvartz
Copy link
Author

Is the struct immutable ?

Yes, but in fact in Mapster it works wit it as a mutable object.
Mapping process can be retarget into an "Records mapping algorithm"

Comment on lines +37 to +73
/// <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);
}
}
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@stagep @DevTKSS I also think these two adapters should be marked as deprecated and then removed in future versions.
But we'll need to double-check that the generic version works in all cases.

Copy link
Author

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. 😂

@DocSvartz
Copy link
Author

Are there any other types that would be considered immutable?

There's another problem. There are settings that make any type seem "immutable".

.ConstructUsing()

Maybe:
.MapToTargetWith()
.BeforeMapping() .AfterMapping();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Adapt(source, destination) does not work when destination is a C# record

1 participant