You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
ILSpy version: 4.0.0.4319-beta2
Target .NET Runtime: v4.0.30319
Compiled code C# language version: 7.3
Decompiled code C# language version: tried 7.3 and 5.0 with same results
ILSpy sometimes seems to decompile subscription to an event of a newly created object as a field initializer, which is not acceptable by any currently available version of C#.
using System;namespaceTestDecompiler{publicclassFoo{publicdelegatevoid BarEvent(stringbaz);publicstringBaz;publiceventBarEventOnBar;publicvoidBar(){this.OnBar?.Invoke(Baz);}}internalclassProgram{publicstaticvoidMain(string[]args){
GetFoo().Bar();}publicstatic Foo GetFoo(){returnnew Foo
{Baz="Baz!",OnBar=OnBar// <== This is an error.};}publicstaticvoidOnBar(stringbaz){
Console.WriteLine(baz);}}}
However if the new object creation is moved from GetFoo() method to the main():
Yes. It is on by default, so I never think about it. Still even if it is an optimized code maybe ILSpy could check type of field that is in the initializer and move subscription to events right after initialization?
ILSpy version: 4.0.0.4319-beta2
Target .NET Runtime: v4.0.30319
Compiled code C# language version: 7.3
Decompiled code C# language version: tried 7.3 and 5.0 with same results
ILSpy sometimes seems to decompile subscription to an event of a newly created object as a field initializer, which is not acceptable by any currently available version of C#.
Original code:
Decompiled code (2 files combined):
However if the new object creation is moved from GetFoo() method to the main():
it decompiles fine (no longer uses field initializers):
EDIT: Throwing in some extra cases
Original code:
Decompiled code:
The text was updated successfully, but these errors were encountered: