public TResult MapElement()

in EnvDTE.Host/Manager/AstManager.cs [34:60]


        public TResult MapElement<TNode, TElement, TResult>(
            int id,
            [NotNull] Func<TNode, TResult> psiMapper,
            [NotNull] Func<TElement, TResult> declaredElementMapper,
            [NotNull] Func<IArrayType, TResult> typeMapper
        )
            where TNode : ITreeNode
            where TElement : IDeclaredElement =>
            TryMapWith(id, PsiContainer, psiMapper)
            ?? TryMapWith(id, DetachedAstContainer, declaredElementMapper)
            ?? TryMapWith(id, TypeContainer, typeMapper)
            ?? throw new InvalidOperationException($"Attempted to map non-existing element with id {id}");

        [CanBeNull]
        private static TResult TryMapWith<TNode, TBaseNode, TResult>(
            int id,
            [NotNull] AstContainer<TBaseNode> container,
            [NotNull] Func<TNode, TResult> mapper
        )
            where TNode : TBaseNode
            where TBaseNode : class
        {
            var node = container.TryGetElement(id);
            if (node == null) return default;
            if (node is TNode correct) return mapper(correct);
            throw new InvalidOperationException($"Wrong node type. Expected {typeof(TNode)}, actual {node.GetType()}");
        }