1

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: enter image description here

.NET 4.6.2: enter image description here

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!

pdalbe01
  • 828
  • 7
  • 17
  • Yes because in your example you are using a delegate and the api changed for how to access them. There is a question about that here somewhere in my favorites but I'm on my phone. But you could change it to `Expression> x = f => f.DoSomething();` and access the MethodInfo that way (which should be preferable) – pinkfloydx33 Feb 21 '17 at 21:38
  • One of the answers here should help, though you should try doing this with an `Expression>` and not `Expression>` http://stackoverflow.com/a/26976055/491907 You are trying to use a Method Group which is essentially a delegate and you have to do some extra "unwrapping" to get to the method – pinkfloydx33 Feb 21 '17 at 21:47
  • Also to specifically answer your question about documentation, the change came about in 4.5 but I doubt this was explicitly documented as its sort of an abuse of the system. If anything you'd find it with regards to `Delegate`s or to `MethodInfo.CreateDelegate` which is what it was changed to use – pinkfloydx33 Feb 21 '17 at 22:08
  • @pinkfloydx33 Yes, actually, that's more or less the body of my GetMethodInfo body, just safer using as and not direct casts. But do you know of any document MS produced about this change? – pdalbe01 Feb 21 '17 at 22:09

1 Answers1

4

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).

Jon Hanna
  • 110,372
  • 10
  • 146
  • 251
  • So it's not a change in .NET, it's being compiled differently? We were using .NET 4 with VS2015 and now it's .NET 4.6.2 also with VS2015. Wouldn't the compiler be the same? – pdalbe01 Feb 23 '17 at 19:16
  • The compiler is the same, but it can't use `MethodInfo.CreateDelegate()` if it's compiling against an earlier framework as it doesn't exist. Compare with how with 4.6 a call to a `params` method with empty `params` is compiled as a call to `Array.Empty()` but not with earlier framework versions where that doesn't exist. – Jon Hanna Feb 23 '17 at 19:53