.NET 4和.NET 4.6.2之间的表达主体差异 [英] Expression Body Differences Between .NET 4 and .NET 4.6.2

查看:108
本文介绍了.NET 4和.NET 4.6.2之间的表达主体差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的团队正在将应用程序从.NET 4.0升级到4.6.2。是的,聚会晚了,但是迟到总比没有好。



在我们的应用程序中,有一个扩展方法可以从返回的方法返回MethodInfo。表达。换句话说,如果我们有:

 公共类Foo 
{
public void DoSomething(){ }
}

然后有一个表达式

  Expression< Func< Foo,Action>> = f => f.DoSomething; 

然后我们的扩展方法将返回方法DoSomething()的MethodInfo



该代码在.NET 4.0中很好用,但在.NET 4.6.2中不起作用。此后,我将代码更改为可以使用,但是我的问题是,有人知道.NET 4.5、4.5.1、4.5.2、4.6、4.6.1和4.6.2的发行说明中的​​哪些地方可以记录吗?我已经多次阅读和搜索它们,没有任何内容。



这些是我一直在浏览的发行说明:






  • .NET 4.6.2:



    <我知道.NET引入了一种执行此操作的方法。我对更改的记录位置(而不是解决方案)(我已经有了)感兴趣。



    在此先感谢我们的帮助!

    解决方案

    编译器使用的 MethodInfo.CreateDelegate()方法而不是 Delegate.CreateDelegate()在.NET 4.5中引入



    C#表达式的已记录行为

      Expression< Func< Foo,Action>> e = f => f.DoSomething; 

    是将创建一个表示 Func< Foo,Action> ,如果对其进行编译和调用,将采用 Foo 并返回 Action 调用 .DoSomething()在该 Foo 上。此行为没有更改。由于记录的行为没有变化,因此很有可能没有变化的记录。 (如果已知会引起问题,则记录了此类更改,但并非总是如此)。


    My team is in the midst of upgrading an application from targeting .NET 4.0 to 4.6.2. Yes, late to the party(ies), but better late than never.

    In our application, there is an extension method that returns the MethodInfo of the returned method from an expression. In other words, if we have:

    public class Foo
    {
        public void DoSomething() { }
    }
    

    and then had an expression

    Expression<Func<Foo, Action>> = f => f.DoSomething;
    

    then our extension method would return the MethodInfo of the method DoSomething()

    The code worked great in .NET 4.0, but doesn't work in .NET 4.6.2. I've since changed the code to work, but my question is does anyone know where in the release notes from .NET 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1 and 4.6.2 would this be documented? I've read and searched through them multiple times without anything.

    These are the release notes I've been looking through:

    When you compare the local variables when debugging, you can see how the method bodies of the expressions differ between .NET 4.0 and 4.6.2:

    .NET 4.0:

    .NET 4.6.2:

    I'm aware that .NET introduced a method that does this; I'm interested in where the change is documented as opposed to the solution (which I already have).

    Thanks in advance for our help!

    解决方案

    The MethodInfo.CreateDelegate() method that the compiler is using instead of Delegate.CreateDelegate() was introduced with .NET 4.5

    The documented behaviour of the the C# expression

    Expression<Func<Foo, Action>> e = f => f.DoSomething;
    

    is that it will create an expression representing a Func<Foo, Action> that if compiled and invoked will take a Foo and return an Action that calls .DoSomething() on that Foo. This behaviour has not changed. As there was no change to the documented behaviour, there's quite likely no documentation of the change. (Such changes are documented if they're known to cause issues, but not always).

    这篇关于.NET 4和.NET 4.6.2之间的表达主体差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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