How do I express a void method call as the result of DynamicMetaObject.BindInvokeMember?

前端 未结 4 1782
栀梦
栀梦 2021-01-31 01:47

I\'m trying to give a short example of IDynamicMetaObjectProvider for the second edition of C# in Depth, and I\'m running into issues.

I want to be able to express a voi

4条回答
  •  有刺的猬
    2021-01-31 02:23

    I don't like this, but it seems to work; the real problem seems to be the binder.ReturnType coming in oddly (and not being dropped ("pop") automatically), but:

    if (target.Type != binder.ReturnType) {
        if (target.Type == typeof(void)) {
            target = Expression.Block(target, Expression.Default(binder.ReturnType));
        } else if (binder.ReturnType == typeof(void)) {
            target = Expression.Block(target, Expression.Empty());
        } else {
            target = Expression.Convert(target, binder.ReturnType);
        }
    }
    return new DynamicMetaObject(target, restrictions);
    

提交回复
热议问题