none
lambda func exception that maybe is .Net Framework 's bug? RRS feed

  • Question

  •  class Program
        {
            static void Main(string[] args)
            {
                var t = new TestImpl();
                Console.WriteLine(t.Test(TestEnum.Value));
            }
        }
    
        public class AbstractTest<T>
        {
            public virtual T TestBase(TestEnum v)
            {
                return default(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)

    Who can tell me why? what's the theory?


    自省,谦虚

    Tuesday, September 17, 2013 9:35 AM

Answers

  • Is this all code you have that throws that exception?

    Don't actually know why you get that exception, because there is also null reference error at the delegate because TestBase returns default of T and in the case of class that is null reference. So the line return result.id should throw exception stating that you access null reference.

    Func<int> f = () =>
    {
        var result = base.TestBase(ev);
        return result.Id;
    };


    • Marked as answer by Damon Bu - MSFT Wednesday, September 25, 2013 9:17 AM
    Tuesday, September 17, 2013 12:09 PM
  • Is it BadImageFormatException?

    If it is a standalone application, you shouldn't face this problem.

    But still, try changing the "Platform target" from "Any CPU" to x86 or x64 as you require.

    (1) Open "Solution Explorer"

    (2) Open "Properties" of the project

    (3) "Build" tab

    (4) Platform target

    Tuesday, September 17, 2013 12:57 PM

All replies

  • Is this all code you have that throws that exception?

    Don't actually know why you get that exception, because there is also null reference error at the delegate because TestBase returns default of T and in the case of class that is null reference. So the line return result.id should throw exception stating that you access null reference.

    Func<int> f = () =>
    {
        var result = base.TestBase(ev);
        return result.Id;
    };


    • Marked as answer by Damon Bu - MSFT Wednesday, September 25, 2013 9:17 AM
    Tuesday, September 17, 2013 12:09 PM
  • Is it BadImageFormatException?

    If it is a standalone application, you shouldn't face this problem.

    But still, try changing the "Platform target" from "Any CPU" to x86 or x64 as you require.

    (1) Open "Solution Explorer"

    (2) Open "Properties" of the project

    (3) "Build" tab

    (4) Platform target

    Tuesday, September 17, 2013 12:57 PM