2016-05-17 6 views
1

アプリケーションの起動時に、新しいMapperConfigurationを作成し、そのプロファイルを使用するように構成します。一部のプロファイルの内部では、カスタムValueResolverを使用してデータ変換を行います。MapperConfigurationからカスタムValueResolverを決定する

設定が完了したら、次のように実行して、マップされたすべてのプロパティとタイプを取得できます。

この例では、最初のマッピングしか取得できませんが、これは単なる例です。私が抱えている問題は、特定のマッピングで使用されるカスタムリゾルバを決定することです。

カスタムリゾルバを持つ任意のマッピングがDelegateBasedResolver <の値リゾルバを持っているように見えるという私を見つけた>

var resolver = (from a in mapping.GetSourceValueResolvers() 
       let b = a.GetType() 
       where b.IsGenericType 
       && b.GetGenericTypeDefinition() == typeof(DelegateBasedResolver<>) 
       select a).FirstOrDefault(); 

これは動作するようですとカスタムリゾルバをマッピングリゾルバに接続されるたびにではありませんヌル。しかし、これは私が得た限りです。私は上記のマッピングから取得しようとしている何

public class ExampleProfile : Profile 
{ 
    protected override void Configure() 
    { 
     CreateMap<ExampleDto, ExampleSummary>() 
      .ForMember(a => a.Id, a => a.MapFrom(b => b.ExampleId)) 
      .ForMember(a => a.IsEnabled, a => a.ResolveUsing<CustomStringToBooleanResolver>().FromMember(b => b.is_enabled)); 
    } 
} 

、どちらかのタイプCustomStringToBooleanResolverかのインスタンスである:

のは、私はこのような単純なマッピングがあるとしましょう。私はリゾルバを実行する方法を知っていると思うが、それは私がしたいことではない。誰でもアイデアはありますか?

答えて

0

よく掘り下げた後、私は実際に4.2.1から最新のベータ版5.0にアップグレードすることでこれに対する答えを見つけました。

var entry = new TranslationEntry(sourceType, destinationType); 
var propertyInfo = mapping.SourceMember as PropertyInfo; 
if (propertyInfo == null && mapping.ValueResolverConfig != null) 
{ 
    entry.Resolver = mapping.ValueResolverConfig.Type; 
    var expression = mapping.ValueResolverConfig.SourceMember.Body as MemberExpression; 

    if (expression != null) 
     propertyInfo = expression.Member as PropertyInfo; 
} 

if (propertyInfo == null) 
    return; 

entry.DestinationProperty = mapping.DestinationProperty.Name; 
entry.DestinationPropertyType = mapping.DestinationPropertyType; 
entry.SourceProperty = propertyInfo.Name; 
entry.SourcePropertyType = propertyInfo.PropertyType; 

コードは次のようになります

関連する問題