0

Consider I have the following code:

try
{
    using (var scope = CreateDependencies())
    {
        ...
    }
}

When I decompile it:

try
{
    ILifetimeScope scope;
    int num1;
    if ((uint) (num1 - 1) > 2U)
        scope = Host.CreateDependencies();
    try
    {
        ...
    }
    ...
}
....

Why the hell there is addtional if that makes completely no sense?

The ILCode produced by ILDasm:

.try
{
    IL_00b8:  ldloc.0
    IL_00b9:  ldc.i4.1
    IL_00ba:  sub
    IL_00bb:  ldc.i4.2
    IL_00bc:  ble.un.s   IL_00ca
    IL_00be:  ldarg.0
    IL_00bf:  ldloc.1
    IL_00c0:  call       instance class [Autofac]Autofac.ILifetimeScope MyApp.Host::CreateDependencies()
    IL_00c5:  stfld      class [Autofac]Autofac.ILifetimeScope MyApp.Host/'<RunAsync>d__7'::'<scope>5__2'
    IL_00ca:  nop
    .try
    {
        ...
    }
    ....
}
pwas
  • 3,225
  • 18
  • 40
  • 2
    are you *very* sure the code you're decompiling is up to date with the source? is there any chance there *was* a conditional in the past? are you running any extra build rewriter tools like postsharp? any obfuscation tools? is this perhaps in an iterator block (`yield return`) or similar compiler-generated state machine? – Marc Gravell Nov 16 '17 at 17:13
  • lets see the whole function - i agree with @MarcGravell, this looks like a yield returny thing – pm100 Nov 16 '17 at 17:16
  • 1
    @pm100 could also be an `async` thing - and a couple of other things that create state machines – Marc Gravell Nov 16 '17 at 17:17
  • Yes, am the very sure dat source is up-to-date with assembly. And no, there were no conditional before in this place. No, no extract rewrites or sth like postharp. No obusfaction tools. Just .net netstandard 2.0 library project. The following code is inside `async` method, if it matters. – pwas Nov 16 '17 at 17:17
  • 3
    @pwas yes, that matters; that's the answer to your question; that's the tracking for the `async` state machine state – Marc Gravell Nov 16 '17 at 17:18
  • @MarcGravell even if it is using unassigned variable? – pwas Nov 16 '17 at 17:19
  • @pwas yes; the `.locals init` means it is wiped to zero; at the IL level that has a predictable value, even if there isn't a `stloc` – Marc Gravell Nov 16 '17 at 17:20
  • @MarcGravell I see.- got it. it looks strange, but somehow works ;) – pwas Nov 16 '17 at 17:25
  • 1
    it is an interesting exercise to read through the code that async / await compiles to. It removes a lot of the voodoo magic sheen. There are some good blog writeups too - google around a bit – pm100 Nov 16 '17 at 17:30

0 Answers0