Mapster在映射隐藏属性时提供不明确的映射



添加了一个小提琴手: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.的引擎盖下面

找到了一个变通方法(不漂亮(

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 Briggs的建议,即使用方法返回Id而不是ToString((,这确实有效,而且很可能更快、更安全。

最新更新