简体   繁体   中英

lambda func exception ,that maybe is .Net Framework 's bug?

class Program
{
    static void Main(string[] args)
    {
        var t = new TestImpl();
        Console.WriteLine(t.Test(TestEnum.Value));
    }
}

public class AbstractTest<T> where T:new()
{
    public virtual T TestBase(TestEnum v)
    {
        return new T();
    }
}

public class Product
{
    public int Id { get; set; }
}

public enum TestEnum
{
     Value
}
public class TestImpl : AbstractTest<Product>
{
    public int Test(TestEnum ev)
    {
        Func<int> f = () =>
        {
            var result = base.TestBase(ev);
            return result.Id;
        };
        return f();
    }
}

Hi All:

I have a issue like the code (run on the .net4.0).

it will throw exception:An attempt was made to load a program with an incorrect format. (Exception from HRESULT: 0x8007000B)

if make " var result = base.TestBase(ev);" chanage to " var result = this.TestBase(ev);" this will normal. if not use lambda func ,use common func like this:

 public class TestImpl : AbstractTest<Product>
{
    public int Test(TestEnum ev)
    {
        Func<int> f = () =>
        {
            //var result = base.TestBase(ev);
           // return result.Id;
            return TestResult(ev);
        };
        return f();
    }

    private int TestResult(TestEnum ev)
    {
        var result = base.TestBase(ev);
        return result.Id;
    }
}

that is ok. i think is "base" or "this" point to different case or other .

Who can tell me why and what's happened? what's the theory? thanks.

The error you get normally is a result of a 64 bit application trying to load a 32 bit assembly or vice versa.

However, I could actually reproduce the problem on a machine without .NET 4.5 installed.

The generated IL of the anonymous class seems to be really invalid, because ILSpy even fails to decompile it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM