mercredi 22 février 2017

Anonymous method in static class is non-static? How to invoke it?

I am running the following program on two different machines:

static class Program
{
    static void Main(string[] args)
    {
        Func<int> lambda = () => 5;
        Console.WriteLine(lambda.GetMethodInfo().IsStatic);
        Console.ReadLine();
    }        
}

On one machine, with .NET 4.5 and Visual Studio 2012 installed this prints "true", on another one, with .NET Framework 4.6.2 and Visual Studio 2015 it prints "false".

I thought that anonymous methods were static if they are defined in a static context. Did this change (in a documented way) during some of the last framework updates?

What I need to do, is to use Expression.Call on lambda.GetMethodInfo(), and in the non-static case this requires an instance on which the lambda is defined. If I wanted to use lambda.GetMethodInfo().Invoke I would face the same problem.

How can I get such an instance?





Aucun commentaire:

Enregistrer un commentaire