Input code
Create a new dotnet project called App.csproj with this code:
public class Program
{
public void TestConstructor()
{
Library1.MyStruct c = new Library1.MyStruct(4);
System.Console.WriteLine("Test new struct: " + c);
}
}
Make a separate library project with this struct:
namespace Library1
{
public struct MyStruct
{
public int x;
public MyStruct(int x)
{
this.x = x;
}
}
}
Add a reference from the main App project to the Library1 project,
then compile the solution,
then copy ONLY the App.dll without the Library1.dll to another folder
(this is very important, otherwise the error won't reproduce).
Load the App.dll in ILSpy and ensure that Library1.dll is not listed in ILSpy's assembly list
even after expanding the App.Program class in the tree view,
if it does show Library1, remove it from the list
and ensure that dll is not next to the App.dll in the same folder.
Then open the App.dll > Program > TestConstructor in ILSpy to see the generated code.
It fails in both the desktop app or the vscode extension.
Erroneous output
public class Program
{
public unsafe void TestConstructor()
{
MyStruct val = default(MyStruct);
((MyStruct)(ref val))..ctor(4);
// ((MyStruct)(ref val))._002Ector(4); // <<< NOTE: If you save it as code instead of just previewing the code, it will show this line instead.
Console.WriteLine("Test new struct: " + ((object)(*(MyStruct*)(&val))/*cast due to constrained. prefix*/).ToString());
}
}
Compiler error message:
Invalid expression term 'ref'
The result is almost right,
it knows it should use a constructor,
it knows the type of the variable is MyStruct,
it just fails to write it down correctly.
And it adds the unsafe keyword instead of using the struct normally.
This is what was expected:
public class Program
{
public void TestConstructor()
{
MyStruct val = new MyStruct(4);
Console.WriteLine("Test new struct: " + val);
}
}
Details
- Product in use:
ILSpy version 10.0.1.8346+aad16c66e96eb887eb05887d6b5a9e0522637906
.NET version 10.0.7-servicing.26217.108+b16286c2284fecf303dbc12a0bb152476d662e44
vscode extension "icsharpcode.ilspy-vscode" Version 0.24.0
If you have problems reproducing this, let me know and I can share the full project and more details.
Input code
Create a new dotnet project called
App.csprojwith this code:Make a separate library project with this struct:
Add a reference from the main
Appproject to theLibrary1project,then compile the solution,
then copy ONLY the
App.dllwithout theLibrary1.dllto another folder(this is very important, otherwise the error won't reproduce).
Load the
App.dllin ILSpy and ensure thatLibrary1.dllis not listed in ILSpy's assembly listeven after expanding the
App.Programclass in the tree view,if it does show
Library1, remove it from the listand ensure that dll is not next to the
App.dllin the same folder.Then open the
App.dll > Program > TestConstructorin ILSpy to see the generated code.It fails in both the desktop app or the vscode extension.
Erroneous output
Compiler error message:
The result is almost right,
it knows it should use a constructor,
it knows the type of the variable is
MyStruct,it just fails to write it down correctly.
And it adds the
unsafekeyword instead of using the struct normally.This is what was expected:
Details
ILSpy version 10.0.1.8346+aad16c66e96eb887eb05887d6b5a9e0522637906
.NET version 10.0.7-servicing.26217.108+b16286c2284fecf303dbc12a0bb152476d662e44
vscode extension "icsharpcode.ilspy-vscode" Version 0.24.0
If you have problems reproducing this, let me know and I can share the full project and more details.