添加了一个Fiddler:https://dotnetfiddle.net/RgcDUF
{
public static void Main()
{
Console.WriteLine("Hello World");
TypeAdapterConfig<B, ADto>.NewConfig().Map(dest => dest.Id, src => src.Id);
var b = new B(){Id = 1};
var aDto = b.Adapt<ADto>();
Console.WriteLine(aDto.Id.ToString());
}
}
public class A{
public virtual long Id {get;set;}
}
public class B : A{
public new int Id {get;set;}
}
public class ADto{
public int Id {get; set;}
}
因为我们将Id隐藏在B中,所以当将B映射到ADto时,Mapster给出了发现的模糊映射:
source=B
destination=ADto
type=Map
---> System.Reflection.AmbiguousMatchException: Ambiguous match found.
at System.RuntimeType.GetPropertyImpl(String name, BindingFlags bindingAttr, Binder binder, Type returnType, Type[] types, ParameterModifier[] modifiers)
at System.Type.GetProperty(String name, BindingFlags bindingAttr)
at System.Linq.Expressions.Expression.PropertyOrField(Expression expression, String propertyOrFieldName)
at Mapster.Utils.ExpressionEx.PropertyOrField(Expression expr, String prop)
at System.Linq.Enumerable.Aggregate[TSource,TAccumulate](IEnumerable`1 source, TAccumulate seed, Func`3 func)
at Mapster.Utils.ExpressionEx.PropertyOrFieldPath(Expression expr, String path)
at Mapster.Models.InvokerModel.GetInvokingExpression(Expression exp, MapType mapType)
at Mapster.ValueAccessingStrategy.CustomResolverFn(Expression source, IMemberModel destinationMember, CompileArgument arg)
at Mapster.Adapters.BaseClassAdapter.<>c__DisplayClass4_1.<CreateClassConverter>b__2(Func`4 fn, Expression src)
at System.Linq.Enumerable.SelectManyIterator[TSource,TCollection,TResult](IEnumerable`1 source, Func`2 collectionSelector, Func`3 resultSelector)+MoveNext()
at System.Linq.Enumerable.TryGetFirst[TSource](IEnumerable`1 source, Func`2 predicate, Boolean& found)
at Mapster.Adapters.BaseClassAdapter.CreateClassConverter(Expression source, ClassModel classModel, CompileArgument arg, Expression destination)
at Mapster.Adapters.ClassAdapter.CreateInlineExpression(Expression source, CompileArgument arg)
at Mapster.Adapters.BaseAdapter.CreateInlineExpressionBody(Expression source, CompileArgument arg)
at Mapster.Adapters.BaseAdapter.CreateExpressionBody(Expression source, Expression destination, CompileArgument arg)
at Mapster.Adapters.BaseAdapter.CreateAdaptFunc(CompileArgument arg)
at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)
--- End of inner exception stack trace ---
at Mapster.TypeAdapterConfig.CreateMapExpression(CompileArgument arg)
at Mapster.TypeAdapterConfig.CreateMapExpression(TypeTuple tuple, MapType mapType)
at Mapster.TypeAdapterConfig.CreateDynamicMapExpression(TypeTuple tuple)
at Mapster.TypeAdapterConfig.<GetDynamicMapFunction>b__66_0[TDestination](TypeTuple tuple)
at Mapster.TypeAdapterConfig.<>c__DisplayClass55_0`1.<AddToHash>b__0(TypeTuple types)
at System.Collections.Concurrent.ConcurrentDictionary`2.GetOrAdd(TKey key, Func`2 valueFactory)
at Mapster.TypeAdapterConfig.AddToHash[T](ConcurrentDictionary`2 hash, TypeTuple key, Func`2 func)
at Mapster.TypeAdapterConfig.GetDynamicMapFunction[TDestination](Type sourceType)
at Mapster.TypeAdapter.Adapt[TDestination](Object source, TypeAdapterConfig config)
at Mapster.TypeAdapter.Adapt[TDestination](Object source)
at Program.Main()
Command terminated by signal 6
我知道这不是非常漂亮的代码,但在我清理代码之前,需要在当前工作。是否有一个设置可以让Mapster接受它应该只查看Id而不是窥视B的引擎盖下面。
发布于 2022-10-28 18:19:47
找到了解决办法(不是很漂亮)
using Mapster;
public class Program
{
public static void Main()
{
Console.WriteLine("Hello World");
TypeAdapterConfig<B, ADto>.NewConfig()
.Map(dest => dest.Id, src => int.Parse(src.GetId()));
var b = new B(){Id = 1};
var aDto = b.Adapt<ADto>();
Console.WriteLine(aDto.Id.ToString());
}
}
public class A{
public virtual long Id {get;set;}
}
public class B : A{
public new int Id {get;set;}
public int GetId(){
return Id;
}
}
public class ADto{
public int Id {get; set;}
}
本质上,在src属性上调用.ToString()似乎使得mapster在引擎盖下面看起来不太好看,而且它可以工作。
编辑:更改以反映Luke的建议,即使用一种方法来返回Id,而不是ToString(),这确实有效,而且很可能更快、更安全。
https://stackoverflow.com/questions/74239315
复制相似问题