All contributions

icsharpcode/ILSpy

ILSpy

An open source .NET decompiler that turns compiled assemblies back into readable C# code. It is used to inspect libraries whose source code is not at hand.

Language
C#
GitHub stars
27,138
Merged changes
7

What we changed

We fixed seven cases where the decompiler produced code that did not compile or read worse than the source: from Visual Basic exception filters, through await on a type parameter and with expressions that await, to ?. chains over bool? and nint conditionals.

What this work shows

  • IL analysis and Roslyn patterns
  • async/await state machines
  • Visual Basic exception filters
  • Compile round-trip tests
What was broken

A VB exception filter caught object, not Exception

The VB compiler writes the filter for On Error Resume Next and On Error GoTo as a single and expression, not as the branch chain the C# compiler emits. ILSpy did not know that shape and printed catch (object obj) when (obj is Exception && ...).

How we fixed it

The type test moves into the catch declaration

The decompiler now recognises this pattern, takes the type test out of the condition and prints catch (Exception ex) when (...) with the condition alone. Tests for On Error and Visual Basic try, catch and finally blocks were added.

#4134

What was broken

Decompiled code kept the delegate cache field

With _ = (Action<int>)M; the delegate is created and discarded at once, so the cache field is never read. The transform required exactly one read, so the output kept a field named <>O.<0>__M and the code did not compile.

How we fixed it

The delegate construction instead of the cache check

When the cache field has no reads, the check is replaced by the delegate construction it guarded. The output is new Action<int>(M);, which compiles and does the same.

#4133

What was broken

Conditionals lost nint and their local

After an earlier change in the decompiler, two patterns came out worse than the source. A ?: with nint branches of different types got (IntPtr) casts, and int n = b ? 1 : 0; from code built with -o+ was pasted into every use of n.

How we fixed it

nint and the local stay in place

VisitIfInstruction maps IntPtr to nint when native integers are on, with the same logic VisitBox uses. CopyPropagation no longer copies between a bool and a non-bool variable, so the conditional stays in its local.

#4231 #4232

What was broken

await on a type parameter dropped its operand

With await value, where T is constrained to an interface with GetAwaiter, the constrained call becomes an LdObjIfRef instruction. ExpressionBuilder had no case for it, so the output had an unsupported opcode comment instead of value and did not compile.

How we fixed it

The await operand is translated from its address

The await operand is now translated from its address, the way CallBuilder already does for call receivers. The output is await value; again, with a test in AsyncAwaitPatterns.

#4251

What was broken

A ?. chain over bool? failed to compile

The decompiler turned GetValueOrDefault() on a bool? into == true even inside a ?. chain. The chain ended too early, the result was bool instead of bool?, and the ?? false after it did not compile.

How we fixed it

GetValueOrDefault() stays inside the ?. chain

The call is kept as it is when its target is part of a ?. chain, so the output is c?.Flag.GetValueOrDefault() ?? false. We added a test for the printed output and one that runs both forms on null, false and true.

#4253

What was broken

with plus await left a raw <Clone>$ call

When a with expression's initializer contained an await, Roslyn copies the receiver into a hoisted temporary before the await. The initializer transform rejected that copy, so the output kept a _003CClone_003E_0024() call and separate assignments that did not compile.

How we fixed it

The with expression is rebuilt with its await

ILInlining now inlines copies between two hoisted compiler temporaries and leaves copies of user locals alone. The output is value with { X = await Get() } again, and the same works for object initializers.

#4292

Merged changes 7

  1. #4292 Fix #4019: Recognize with expressions whose initializer awaits Merged 8 November 2026
  2. #4253 Fix #3702: Don't rewrite GetValueOrDefault() to == true in ?. chains Merged 17 October 2026
  3. #4251 Fix #4017: Look through LdObjIfRef on the operand of await Merged 16 October 2026
  4. #4231 Fix #4135: Keep bool-to-int conditional in its local Merged 16 October 2026
  5. #4232 Fix #4137: Keep nint in conditionals with mixed native int branches Merged 11 October 2026
  6. #4133 Remove the delegate cache left by a discarded method-group conversion Merged 19 September 2026
  7. #4134 Recognize a VB exception filter's type test Merged 15 September 2026

What it taught us

Changing one transform in a decompiler easily breaks other patterns, so you have to read the IL that Roslyn and the VB compiler actually emit. Each change has a test that compiles a sample, decompiles it and compares the result with the original.

More about this technology: C# and .NET

Other projects we write code for

Describe your project.

A free quote comes back within an hour during working hours.