dnSpyEx / dnSpy

Unofficial revival of the well known .NET debugger and assembly editor, dnSpy
GNU General Public License v3.0
7.01k stars 459 forks source link

CS4012 Parameters or locals of type 'DefaultInterpolatedStringHandler' cannot be declared in async methods or async lambda expressions. #278

Open threerog opened 11 months ago

threerog commented 11 months ago

dnSpyEx version

6.4.1

Describe the Bug

After decompiling the code with $"{xxxx}" string concatenation in the original code, it is not possible to edit the code and recompile it through dnSpy. The name 'defaultInterpolatedStringHandler' does not exist in the current context

How To Reproduce

$"{xxxx}"

Expected Behavior

.

Actual Behavior

.

Additional Context

No response

GazziFX commented 11 months ago

on dnSpy 6.4.0 im able to edit method (C#), probably you have unresolved assemblies

threerog commented 11 months ago

on dnSpy 6.4.0 im able to edit method (C#), probably you have unresolved assemblies Sorry, the previous description was incorrect. I have updated the correct issues title

threerog commented 11 months ago

original code logger.Warn($"*****{obj.Id}*****{flag}");

dnSpy decompiled code

DefaultInterpolatedStringHandler defaultInterpolatedStringHandler = new DefaultInterpolatedStringHandler(10, 2);
defaultInterpolatedStringHandler.AppendLiteral("*****");
defaultInterpolatedStringHandler.AppendFormatted(obj.Id);
defaultInterpolatedStringHandler.AppendLiteral("*****");
defaultInterpolatedStringHandler.AppendFormatted<bool>(flag);
logger.Warn(defaultInterpolatedStringHandler.ToStringAndClear());

When modifying the method and compiling, dnSpy reports an error CS4012 Parameters or locals of type 'DefaultInterpolatedStringHandler' cannot be declared in async methods or async lambda expressions.

threerog commented 11 months ago

What's even more strange is that I tried to delete the code related to the DefaultInterpolatedStringHandler in dnspy, concatenated it with $"", and after dnspy compiled it normally, I reloaded the DLL file. However, dnspy did not decompile the code that was just concatenated with $"" into the DefaultInterpolatedStringHandler dnspy seems to only decompile the code containing $"" concatenation in the DLL compiled by VS into the DefaultInterpolatedStringHandler, without processing the DLL compiled by dnspy itself

GazziFX commented 11 months ago

Decompilation of interpolated string handler into original expression is not supported yet, because of old decompiler engine. And after editing it becomes normal because dnSpy probably compiled it into older string.Format("*****{0}*****{1}", obj.Id, flag)

P.S. in my app dnSpy compiled it into DefaultInterpolatedStringHandler and it doesn't show it as original $"' string

wappenull commented 3 months ago

+1 to this issue, found it too, but it is not deal breaker. dnSpyEx can display async property vs original one which cant is already a blessing. TY to authors.

My current workaround is to open ILSpy along side and copy cleaner decompiled code from there when recompiling method. The compiled result was ok-ish, and result back into DefaultInterpolatedStringHandler.

(Screenshot: decompiled code from dnSpyEx and ILSpy) image

ElektroKill commented 3 months ago

This will be fixed properly when the decompiler engine is updated to the latest ILSpy version. Currently, the workaround suggested by @wappenull is one of the better ways to get the C# editor working well with modern code.