.NET Linq to Objects weird behavior

I have a BadImageFormatException error in this small code below. I know that itโ€™s not a good practice to write a program this way, but it seems to be a bug in the .NET Framework, and not in my code.

using System.Collections.Generic;
using System.Linq;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            var b = new B();
            var bb = b.Test();
            var bbb = bb.Count();
        }
    }

    class A<T>
    {
        public virtual IEnumerable<T> Test()
        {
            yield return default(T);
        }
    }

    class B : A<int>
    {
        public IEnumerable<int> Test()
        {
            base.Test();
            yield return 0;
        }
    }
}

Any ideas why this is not working?

+5
source share
2 answers

As a side note, you should declare the method B.Test()as an override, but this is another problem.

A line note base.Test();corrects it. Here is my theory.

, B.Test(), . , . , , - .

, :

class B : A<int>
{
    public override IEnumerable<int> Test()
    {
        base.Test();
        yield return 0;
    }
}

, , , . base, . , , - , base . , :

[CompilerGenerated]
private sealed class <Test>d__0 : IEnumerable<T>, IEnumerable, IEnumerator<T>, IEnumerator, IDisposable
{
    bool MoveNext()
    {
        // ...
        base.Test(); // what, base?
        // ...
    }
}

, Reflector, ( , ).

, , :

System.Console.WriteLine("Starting");
using (var e = bb.GetEnumerator())
{
    System.Console.WriteLine(e.MoveNext());
    System.Console.WriteLine(e.Current);
    System.Console.WriteLine(e.MoveNext());
}

. MoveNext() ( ). , , . , , :

            System.Console.WriteLine("Starting");
00000075  mov         ecx,dword ptr ds:[03622088h] 
0000007b  call        63474D1C 
00000080  nop 
            using (var e = bb.GetEnumerator())
00000081  mov         ecx,dword ptr [ebp-44h] 
00000084  call        dword ptr ds:[001E0020h] 
0000008a  mov         dword ptr [ebp-58h],eax 
0000008d  mov         eax,dword ptr [ebp-58h] 
00000090  mov         dword ptr [ebp-48h],eax 
            {
00000093  nop 
                System.Console.WriteLine(e.MoveNext());
00000094  mov         ecx,dword ptr [ebp-48h] 
00000097  call        dword ptr ds:[001E0024h]     // ERROR!!!!!!!!!!!!!!!!
0000009d  mov         dword ptr [ebp-5Ch],eax 
000000a0  mov         ecx,dword ptr [ebp-5Ch] 
000000a3  call        63A48640 
000000a8  nop 
                System.Console.WriteLine(e.Current);
000000a9  mov         ecx,dword ptr [ebp-48h] 
000000ac  call        dword ptr ds:[001E0028h] 
000000b2  mov         dword ptr [ebp-60h],eax 
000000b5  mov         ecx,dword ptr [ebp-60h] 
000000b8  call        63A49388 
000000bd  nop 

, , - , , , base.

+3

. base.Test(). Test .

class A<T>
    {
        public virtual IEnumerable<T> Test()
        {
            yield return default(T);
        }
    }

    class B : A<int>
    {
        public new IEnumerable<int> Test()
        {
            this.MyDelegate();           
            yield return 0;
        }
        private void MyDelegate()
        {
            base.Test();
        }
    }
    public class CompassModel
    {
        public void GetTeamLeadData()
        {
            var b = new B();
            var bb = b.Test();
            var bbb = bb.Count();
        }
    }

. http://msdn.microsoft.com/query/dev10.query?appId=Dev10IDEF1&l=EN-US&k=k(EHBADIMAGEFORMAT);k(TargetFrameworkMoniker-%22.NETFRAMEWORK%2cVERSION%3dV4.0%22);k(DevLang-CSHARP)&rd=true

0

All Articles