Moq和反射,将动态生成的表达式树/lambda传递给moq [英] Moq and reflection, passing dynamically generated expression tree / lambda to moq

查看:102
本文介绍了Moq和反射,将动态生成的表达式树/lambda传递给moq的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以编写如下代码.我正在尝试将Moq与作为测试框架的一部分反映的对象一起使用.下面的代码从Moq引发了一个未处理的表达式类型:'Goto'"异常,我想这是与众不同的.看起来好像应该可以工作!

Is it possible to write code like the following. I'm trying to using Moq with objects that I'm reflecting on as part of a testing framework. The code below raises a "Unhandled expression type: 'Goto'" exception from Moq, which I guess is expecting something different. It kind of looks like it should work though!

    private void button1_Click(object sender, EventArgs e)
    {
        Ifoo  = foo Foo();

        // Create input parameter for lambda
        ParameterExpression value = Expression.Parameter(typeof(IFoo), "value");

        // create return statement for lambda
        Expression setupProperty = Expression.Return(Expression.Label(), Expression.Property(value, "Bar"), typeof(string));

        // convert expression to lambda (should now be the equivalent of "v => v.Bar")
        var func = Expression.Lambda<Func<IFoo, string>>(setupProperty, value);//.Compile();
        //string s = func(foo); // this bit works fine in .Compile() is included

        var mockFoo = new Mock<IFoo>();

        mockFoo.SetupProperty(func); // exception thrown by moq here, obviously isn't exactly the same as "v => v.Bar"
        mockFoo.Object.Bar = "Burge+";
    }

谢谢!

推荐答案

好的,这是可能的,这里是正确的代码.

Ok, this is possible, here is the corrected code.

// Create input parameter for lambda 
ParameterExpression value = Expression.Parameter(typeof(IFoo), "value"); 

// create return statement for lambda 
Expression setupProperty = Expression.Property(value, "Bar"); 

// convert expression to lambda (should now be the equivalent of "v => v.Bar") 
var func = Expression.Lambda<Func<IFoo, string>>(setupProperty, value);

var mockFoo = new Mock<IFoo>(); 
mockFoo.SetupProperty(func); // this works now
mockFoo.Object.Bar = "Burge+"; 

我通过使用以下代码从lambda创建表达式来研究了这一点

I investigated this by creating an expression from a lambda using the code below

Expression<Func<IFoo, string>> setupBar = v => c.Bar;

然后我在vs 2010的调试器中查看了此内容.表达式具有调试视图",该视图显示了表达式的文本表示形式,因此可以在该表达式或类似内容上添加监视.以上是

I then looked at this in the debugger in vs 2010. Expressions have a "Debug View" that shows a text representation of the expression so it is possible to add a watch on that or something similar. The above comes out as

 .Lambda #Lambda1<System.Func`2[WindowsFormsApplication1.IFoo,System.String]>(WindowsFormsApplication1.IFoo
 $v) {
   $v.Bar
 }

我查看了一下,试图找出什么表达式可以实现,然后创建了一个表达式并在调试器中进行了比较.

I looked at this and tried to work out what Expressions would make this, then created an expression and compared it in the debugger.

对我来说有趣的是,尽管此表达式返回一个值,但没有赋值或return语句.我想这一定是隐性的.

The interesting thing for me is that although this expression returns a value there is no assignment or return statement. I guess this must be implicit somehow.

这篇关于Moq和反射,将动态生成的表达式树/lambda传递给moq的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆