Я нахожу способ получить то, что хочу. Я должен был получить доступ к частной собственности, как это:
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);
}