Как сохранить ссылку по свойству - PullRequest
0 голосов
/ 19 апреля 2019

Я настраиваю профиль автоматического обработчика, чтобы сопоставить элемент Revit (из их API) с каким-то пользовательским элементом. Их API получил циклические ссылки (1 элемент => n параметров, 1 параметр => 1 элемент). Я вызываю методы PreserveReference (). Но это не похоже на работу, потому что у меня есть StackOverflowException. Итак, мне интересно, как работает PreserveReference? Могу ли я указать свойство для проверки равенства вместо использования ссылок?

public class Element
{
    public int Id { get; set; }

    public List<Parameter> Parameters { get; set; }

    public override bool Equals(object obj)
    {
        return obj is Element element && Id == element.Id;
    }

    public override int GetHashCode()
    {
        // ReSharper disable once NonReadonlyMemberInGetHashCode
        return Id;
    }
}

public class Parameter
{
    public int Id { get; set; }

    public Definition Definition { get; set; }

    public Element Element { get; set; }
}


profile.CreateMap<Element, Models.Element>()
            .ForMember(element => element.Id, expression => expression.MapFrom(element => element.Id.IntegerValue))
            .IncludeAllDerived()
            .PreserveReferences();

profile.CreateMap<Parameter, Models.Parameter>()
            .ForMember(parameter => parameter.Id, expression => expression.MapFrom(parameter => parameter.Id.IntegerValue))
            .IncludeAllDerived()
            .PreserveReferences();

1 Ответ

0 голосов
/ 19 апреля 2019

Я нахожу способ получить то, что хочу. Я должен был получить доступ к частной собственности, как это:

public static T GetSource<T>(this ContextCacheKey contextCacheKey)
    {
        var source = (T)typeof(ContextCacheKey).GetField("_source", BindingFlags.NonPublic | BindingFlags.GetField | BindingFlags.Instance)?.GetValue(contextCacheKey);

        return source;
    }

    public class ElementConverter : IValueConverter<Element, Models.Element>
    {
        public Models.Element Convert(Element sourceMember, ResolutionContext context)
        {
            var a = (Models.Element)context.InstanceCache.FirstOrDefault(kvp => kvp.Key.GetSource<Element>().Id.Equals(sourceMember.Id)).Value ?? context.Mapper.Map<Models.Element>(sourceMember);

            return a;
        }
    }

Простое решение было бы так, чтобы Source и DestinationType были доступны в структуре ContextCacheKey:

public struct ContextCacheKey : IEquatable<ContextCacheKey>
{
    public static bool operator ==(ContextCacheKey left, ContextCacheKey right) => left.Equals(right);
    public static bool operator !=(ContextCacheKey left, ContextCacheKey right) => !left.Equals(right);

    public readonly object _source;
    public readonly Type _destinationType;

    public ContextCacheKey(object source, Type destinationType)
    {
        _source = source;
        _destinationType = destinationType;
    }

    public override int GetHashCode() => HashCodeCombiner.Combine(_source, _destinationType);

    public bool Equals(ContextCacheKey other) =>
        _source == other._source && _destinationType == other._destinationType;

    public override bool Equals(object other) => 
        other is ContextCacheKey && Equals((ContextCacheKey)other);
}
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...