0

以前のバージョンの AutoMapper では、特定のメンバーを条件付きでマップする/マップしないための汎用 PreCondition を作成しました。以下のコードに示すように、メンバー名の配列を Map オプションに追加し、現在マップされているメンバーに対してこのリストをチェックすることで、これを実現しました。

残念ながら、MemberName は ResolutionContext で公開されなくなったため、現在どのメンバーがマップされているのかわかりません。新しいスリム化された ResolutionContext には、この情報につながるものは何も見つかりませんでした。

多くの特定の PreCondition ケースを記述できることはわかっていますが、これを無数の型に使用しています。現在の MemberName を取得する他の方法を知っている人はいますか?

using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;

public class AutoMapperTest
{
    public class SomeSourceType
    {
        public string String1;
        public string String2;
        public string String3;
    }

    public class SomeDestinationType
    {
        public string String1;
        public string String2;
        public string String3;
    }

    public void Test()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<SomeSourceType, SomeDestinationType>();

            cfg.ForAllMaps((Action<TypeMap, IMappingExpression>)((typeMap, map) =>
            {
                map.ForAllMembers(opt =>
                {
                    opt.PreCondition(context =>
                    {
                        var optionsItems = context.Options.Items;
                        var propertiesToMap = (IEnumerable<string>)(optionsItems.Keys.Contains("PropertiesToMap") ? optionsItems["PropertiesToMap"] : null);

                        // The following line no longer works because MemberName is no longer exposed!
                        return (propertiesToMap == null) || propertiesToMap.Contains(context.MemberName);
                    });
                });
            }));
        });

        var source = new SomeSourceType() { String1 = "Hello", String2 = "World" };

        // This will map ALL properties.
        var dest = Mapper.Map<SomeSourceType, SomeDestinationType>(source);

        // This will only map String1 and String3.
        dest = Mapper.Map<SomeSourceType, SomeDestinationType>(source, opts => opts.Items.Add("PropertiesToMap", new string[] { "String1", "String3" }));
    }
}
4

1 に答える 1

1

あなたはほとんどそこにいました。"opt" パラメーターは typeIMemberConfigurationExpressionで、宛先メンバーをプロパティとして含みます。更新された例は次のとおりです。

using System;
using System.Collections.Generic;
using System.Linq;
using AutoMapper;

public class AutoMapperTest
{
    public class SomeSourceType
    {
        public string String1;
        public string String2;
        public string String3;
    }

    public class SomeDestinationType
    {
        public string String1;
        public string String2;
        public string String3;
    }

    public void Test()
    {
        Mapper.Initialize(cfg =>
        {
            cfg.CreateMap<SomeSourceType, SomeDestinationType>();

            cfg.ForAllMaps((Action<TypeMap, IMappingExpression>)((typeMap, map) =>
            {
                map.ForAllMembers(opt =>
                {
                    opt.PreCondition(context =>
                    {
                        var optionsItems = context.Options.Items;
                        var propertiesToMap = (IEnumerable<string>)(optionsItems.Keys.Contains("PropertiesToMap") ? optionsItems["PropertiesToMap"] : null);

                        return (propertiesToMap == null) || propertiesToMap.Contains(opt.DestinationMember.Name);
                    });
                });
            }));
        });

        var source = new SomeSourceType() { String1 = "Hello", String2 = "World" };

        var dest = Mapper.Map<SomeSourceType, SomeDestinationType>(source);

        dest = Mapper.Map<SomeSourceType, SomeDestinationType>(source, opts => opts.Items.Add("PropertiesToMap", new string[] { "String1", "String3" }));
    }
}
于 2016-11-21T15:15:43.197 に答える