get different result from extension method
first: public static TDest MapAdapt<TDest>(this object source, TDest dest) { return source.Adapt(dest); }
second: public static TDest MapAdapt<TSource, TDest>(this TSource source, TDest dest) { return source.Adapt(dest); }
first method get result like:source.Adapt<TDest>(); second method works well.
Bug?
@lizhanglong What version of Mapster are you using? Try the latest prerelease.
I tested this with versions 7.3 and 7.4, and the issue persisted. It worked fine with version 7.4.2-pre02. However, versions 7.4 and above do not support the .NET Framework.
This is related to the definition of the Type of the generic parameter.
Mapster does not have a signature such Adapt<TDest>(this object source, TDest dest), there is only Adapt<TSource,TDestination>.
For your first case, a conversion from Adapt<object, TDestination> is obtained.
Which actually does not make sense in the Object type there is no internal state that could be transferred to the TDestination type :)
If the source type can really be found out only at runtime, then in your case you should use the following adapter
Adapt(this object source, object destination, Type sourceType, Type destinationType).
public static TDest MapAdapt<TDest>(this object source, TDest dest)
{
if (source is null || source.GetType() == typeof(object))
return dest; // infinite loop if source is real instance of Object var source = new Object()
return (TDest)source.Adapt(dest, source.GetType(), typeof (TDest));
}