为什么VS2010在链接具有动态参数的方法时失败 [英] Why VS2010 Intelissense fails when chaining methods with dynamic args

查看:91
本文介绍了为什么VS2010在链接具有动态参数的方法时失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一些解释你们C#4.0动态的专家。

I would like some explanation of you guys expert in C# 4.0 dynamic.

我有一个流利的生成器类,以帮助配置对象,然后创建它。这个接口有一个方法SetParameters(...):

I have a fluent builder class to help configure an object before creating it. This interface has a method SetParameters(...):

    public FluentBuilder<TInterface> SetParameters(dynamic parameters)
    {
        _parameters = parameters;
        return this;
    }



我这样做以消耗流畅的界面:

I'm doing this to consume the fluent interface:

                var order = new Order();

                /* Setting up parameters */
                dynamic parameters = new ExpandoObject();
                parameters.Transaction = transactionObj;
                parameters.CurrentPrincipal = Thread.CurrentPrincipal;

                var proxiedOrder = ObjectProxyFactory
                    .Configure<IOrder>(order)
                    .FilterMethods(o => o.InsertOrder())
                    .AddPreDecoration(AppConcerns.JoinSqlTransaction)
                    .AddPreDecoration(AppConcerns.EnterLog)
                    .AddPostDecoration(AppConcerns.ExitLog)
                    .AddPostDecoration(AppConcerns.SecurityCheck)
                    .SetParameters(parameters)
                    .Teste() //this method doesn't exist in the fluent builder
                    .CreateProxy();

                var result = proxiedOrder.InsertOrder();

如上面的代码片段中所说,在流畅接口中不存在称为Teste ,但是intelissense允许在我调用SetParameters之后写任何方法,像返回动态,但是如代码中所示,SetParameters返回不是动态的FluentInterface。

As commented in the above snippet, the method called Teste() doesn't exists in the fluent interface, but intelissense allow write anymethod after I call SetParameters like it returning dynamic, but as you see in code, SetParameters returns FluentInterface that is not dynamic.

上面的代码编译成功在运行时将失败,因为在运行时方法Teste()将不会在FluentBuilder类中找到。

The code above compiles sucessfully by in runtime will fail because in runtime the method Teste() will not be found in FluentBuilder class.

要在设计时解决这个问题,并获得正确的Intelissense ,我需要将参数转换为ExpandoObject类:

To resolve this problem in design-time, and to get correct Intelissense, I need to cast the parameter to the ExpandoObject class:

                    var proxiedOrder = ObjectProxyFactory
                    .Configure<IOrder>(order)
                    .FilterMethods(o => o.InsertOrder())
                    .AddPreDecoration(AppConcerns.JoinSqlTransaction)
                    .AddPreDecoration(AppConcerns.EnterLog)
                    .AddPostDecoration(AppConcerns.ExitLog)
                    .AddPostDecoration(AppConcerns.SecurityCheck)
                    .SetParameters((ExpandoObject)parameters) //cast to ExpandoObject
                    .Teste() //now intelissense is giving me an "red" error and solution will not compile
                    .CreateProxy();

                var result = proxiedOrder.InsertOrder();



我发现,任何时候我在任何方法链接中传递C#动态参数,接收动态参数,后续对方法的调用将像返回一个C#动态对象一样,即使方法的返回类型不是动态的。

I've found that, anytime I pass a C# dynamic parameter in any method chaining, after that method receiving the dynamic parameter, the subsequent calls to methods will behave like returning a C# dynamic object, even if the return type of the method it's not dynamic.

这是一个错误?或者这是预期发生的吗?

Is it a bug ? Or is this expected to happens ?

推荐答案

预期会发生。任何涉及动态参数的方法调用都是动态解析的 - 在执行时间之前无法确定确切的重载,因此返回类型在编译时是未知的,因此被视为 dynamic 。在某些情况下,C#编译器可以推断更多的信息(例如,如果它是一个静态方法调用),但为了简单,它不。只有涉及动态值的变量中的少数表达式具有非动态类型。 (从内存中,运算符总是 bool ,并且构造函数总是假定返回正在构造的类型。 )

It's expected to happen. Any method call involving a dynamic argument is resolved dynamically - the exact overload can't be determined until execution time, so the return type is unknown at compile time, so it's treated as being dynamic. In some cases the C# compiler could infer more information (e.g. if it's a static method call) but for simplicity it doesn't. Only a variable few expressions involving dynamic values have non-dynamic types. (From memory, the is operator is always bool, and a constructor is always assumed to return the type being constructed.)

编辑:我终于找到规范引用。从第7.6.5节:

I've finally found the spec reference. From section 7.6.5:


如果以下至少一项成立,则调用表达式是动态绑定的(§7.2.2)

An invocation-expression is dynamically bound (§7.2.2) if at least one of the following holds:


  • 主表达式的编译时类型为动态。

  • 可选参数列表的编译时类型为动态,并且primary-expression没有委托类型。

将调用表达式分类为动态类型的值。

In this case the compiler classifies the invocation-expression as a value of type dynamic.

这篇关于为什么VS2010在链接具有动态参数的方法时失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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