You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Connected.Framework/Connected.Expressions/Evaluation/SubtreeResolver.cs

35 lines
768 B

2 years ago
using System.Linq.Expressions;
using System.Reflection;
using ExpressionVisitor = Connected.Expressions.Visitors.ExpressionVisitor;
namespace Connected.Expressions.Evaluation;
public sealed class SubtreeResolver : ExpressionVisitor
{
private SubtreeResolver(Type type)
{
Type = type;
}
private Type Type { get; }
private Expression Found { get; set; }
public static Expression? Resolve(Expression expression, Type type)
{
var finder = new SubtreeResolver(type);
finder.Visit(expression);
return finder.Found;
}
protected override Expression? Visit(Expression? exp)
{
var node = base.Visit(exp);
if (Found is null && node is not null && Type.GetTypeInfo().IsAssignableFrom(node.Type.GetTypeInfo()))
Found = node;
return node;
}
}