c# - 如何使用表达式树安全地访问可为空对象的路径?

标签 c# lambda path tree expression-trees

当我将反序列化的 XML 结果放入 xsd 生成的对象树中并想使用该树 a.b.c.d.e.f 中的一些深层对象时,如果该查询路径上的任何节点丢失,它将给我异常。

if(a.b.c.d.e.f != null)
    Console.Write("ok");

我想避免像这样为每个级别检查 null:

if(a != null)
if(a.b != null)
if(a.b.c != null)
if(a.b.c.d != null)
if(a.b.c.d.e != null)
if(a.b.c.d.e.f != null)
    Console.Write("ok");

第一个解决方案是实现允许这样做的 Get 扩展方法:

if(a.Get(o=>o.b).Get(o=>o.c).Get(o=>o.d).Get(o=>o.e).Get(o=>o.f) != null)
    Console.Write("ok");

第二种解决方案是实现 Get(string) 扩展方法并使用反射来获得如下所示的结果:

if(a.Get("b.c.d.e.f") != null)
    Console.Write("ok");

第三种解决方案,可能是实现 ExpandoObject 并使用动态类型来获得如下所示的结果:

dynamic da = new SafeExpando(a);
if(da.b.c.d.e.f != null)
    Console.Write("ok");

但是最后两个解决方案没有提供强类型和 IntelliSense 的好处。

我认为最好的可能是第四种可以用表达式树实现的解决方案:

if(Get(a.b.c.d.e.f) != null)
    Console.Write("ok");

if(a.Get(a=>a.b.c.d.e.f) != null)
    Console.Write("ok");

我已经实现了第一个和第二个解决方案。

这是第一个解决方案的样子:

[DebuggerStepThrough]
public static To Get<From,To>(this From @this, Func<From,To> get)
{
    var ret = default(To);
    if(@this != null && !@this.Equals(default(From)))
        ret = get(@this);

    if(ret == null && typeof(To).IsArray)
        ret = (To)Activator.CreateInstance(typeof(To), 0);

    return ret;
}

如果可能,如何实现第 4 种解决方案?

如果可能的话,看看如何实现第三种解决方案也很有趣。

最佳答案

因此,起点是创建一个表达式访问者。这让我们可以找到特定表达式中的所有成员访问。这给我们留下了一个问题,即如何为每个成员访问权限做些什么。

所以第一件事就是递归地访问正在访问成员的表达式。从那里,我们可以使用 Expression.Condition创建一个条件 block ,将处理过的基础表达式与 null 进行比较,并返回 null如果为真,则为原始起始表达式。

请注意,我们需要为成员和方法调用提供实现,但每个过程基本相同。

我们还将添加一个检查,以便底层表达式的值为 null (也就是说,没有实例并且它是一个静态成员)或者如果它是一个不可为 null 的类型,我们只使用基本行为。

public class MemberNullPropogationVisitor : ExpressionVisitor
{
    protected override Expression VisitMember(MemberExpression node)
    {
        if (node.Expression == null || !IsNullable(node.Expression.Type))
            return base.VisitMember(node);

        var expression = base.Visit(node.Expression);
        var nullBaseExpression = Expression.Constant(null, expression.Type);
        var test = Expression.Equal(expression, nullBaseExpression);
        var memberAccess = Expression.MakeMemberAccess(expression, node.Member);
        var nullMemberExpression = Expression.Constant(null, node.Type);
        return Expression.Condition(test, nullMemberExpression, node);
    }

    protected override Expression VisitMethodCall(MethodCallExpression node)
    {
        if (node.Object == null || !IsNullable(node.Object.Type))
            return base.VisitMethodCall(node);

        var expression = base.Visit(node.Object);
        var nullBaseExpression = Expression.Constant(null, expression.Type);
        var test = Expression.Equal(expression, nullBaseExpression);
        var memberAccess = Expression.Call(expression, node.Method);
        var nullMemberExpression = Expression.Constant(null, MakeNullable(node.Type));
        return Expression.Condition(test, nullMemberExpression, node);
    }

    private static Type MakeNullable(Type type)
    {
        if (IsNullable(type))
            return type;

        return typeof(Nullable<>).MakeGenericType(type);
    }

    private static bool IsNullable(Type type)
    {
        if (type.IsClass)
            return true;
        return type.IsGenericType &&
            type.GetGenericTypeDefinition() == typeof(Nullable<>);
    }
}

然后我们可以创建一个扩展方法以使其更容易调用:

public static Expression PropogateNull(this Expression expression)
{
    return new MemberNullPropogationVisitor().Visit(expression);
}

以及接受 lambda 而不是任何表达式,并且可以返回已编译委托(delegate)的方法:

public static Func<T> PropogateNull<T>(this Expression<Func<T>> expression)
{
    var defaultValue = Expression.Constant(default(T));
    var body = expression.Body.PropogateNull();
    if (body.Type != typeof(T))
        body = Expression.Coalesce(body, defaultValue);
    return Expression.Lambda<Func<T>>(body, expression.Parameters)
        .Compile();
}

请注意,为了支持被访问成员解析为不可空值的情况,我们正在更改这些表达式的类型以将它们提升为可空值,使用 MakeNullable .这是这个最终表达式的问题,因为它需要是 Func<T>。 , 如果 T 则不匹配也没有解除。因此,尽管它非常不理想(理想情况下,您永远不会使用不可为 null 的 T 调用此方法,但在 C# 中没有支持此方法的好方法)我们使用该类型的默认值合并最终值,如有必要。

(您可以简单地修改它以接受接受参数的 lambda,并传入一个值,但您也可以轻松地关闭该参数,所以我认为没有真正的理由这样做。)


还值得指出的是,在 C# 6.0 中,当它实际发布时,我们将有一个实际的 null 传播运算符 ( ?. ),这使得所有这些都变得非常不必要。你将能够写:

if(a?.b?.c?.d?.e?.f != null)
    Console.Write("ok");

并具有您正在寻找的语义。

关于c# - 如何使用表达式树安全地访问可为空对象的路径?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30488022/

相关文章:

c# - 转换为 void 返回委托(delegate)的匿名函数无法返回值

python - 了解反斜杠行为 (Windows)

c# - 解析具有多个对象的 Json newtonsoft

c# - 如何在有backgroundWorker的过程中停止执行

c# - 在C#中产生两个列表项的集合差异

javascript - 如何从 lambda 函数返回字符串?

reactjs - Electron react 设置文件下载路径

java - 使用Cloudera快速启动vm和pig shell的Apache Pig输入路径错误

c# - WPF - 将 TextBox 的 FontFamily 绑定(bind)到 ComboBox

c# - 自定义错误页面未在 IIS7 ASP.NET 上显示