ILSpy icon indicating copy to clipboard operation
ILSpy copied to clipboard

Visual Studio WinForms designer unable to display forms exported from VB.NET assembly

Open modz2014 opened this issue 2 years ago • 16 comments

hi im not sure if this is a bug or something but the it doesn't decompile FormMain.Designer.cs from a compiled exe just testing it with my own code i cant still run and build the project no problem but does not show the designer files

modz2014 avatar Jan 07 '22 10:01 modz2014

I just tried this with a brand-new Windows Forms project and Visual Studio 2019 is able to open and display the Designer even though the generated code is not separated. Granted I only tried a very simple project with no changes. Of course VS did take some time until it realized that the file was a WinForms Form. Opening the file in the code-editor helped speed up a lot.

As far as I know, the VS WinForms designer is not required to have a .Designer.cs file, because partial classes were introduced in C# 2.0 and old code from the time before that is still supported, at least it works in my test case.

If you want me to take a closer look and analyze the problem, you will have to provide a binary for me to reproduce this. You can use a private channel: [email protected] is my address. I have some more questions:

  • Which project file format did you use for this when exporting? SDK-style or classic MSBuild? ILSpy will tell you which format it used on the post-export info output.
  • Which language version did you use? Check the selection in the language dropdown.
  • You said it did compile?
  • Which version of VS did you use? 2019? 2022?

Thanks!

siegfriedpammer avatar Jan 24 '22 13:01 siegfriedpammer

i updated it to to visual studio 2022 also i think maybe because there is no FormMain.Designer.cs because it gets generated when compiled maybe thats the problem there

modz2014 avatar Jan 24 '22 14:01 modz2014

As far as I know, there is no generation at compile-time only at design-time. Is it possible for you to share more information and the assembly? Otherwise, I fear there is nothing I can do to improve the situation. Sorry!

siegfriedpammer avatar Jan 24 '22 15:01 siegfriedpammer

Capture

so the compiler is VB.net can be decompiled as C# project

modz2014 avatar Jan 24 '22 16:01 modz2014

here whats happens Build/compiled in Visual Studio 2022 it then Decompiled it using your tool then when back into Visual Studio and i get this error but it will still build/compile just cant edit the Form.Designer.cs

Capture1

modz2014 avatar Jan 24 '22 17:01 modz2014

Yeah, I now understand what the problem is: You are trying to decompile a VB.NET application and of course ILSpy only can generate C# code. However, the VB.NET Forms Designer and C# Forms Designer generate completely different code. So the code is not recognized by the C# version.

ILSpy would have to add more support for this, however, I don't see this happen any time soon.

As soon as you do the following, the errors go away:

  [field: AccessedThroughProperty("Button3")]
  internal virtual Button Button3
  {
	  get;
	  [MethodImpl(MethodImplOptions.Synchronized)] set;
  }

should be replaced with

    internal Button Button3;

Hope this helps!

siegfriedpammer avatar Jan 25 '22 23:01 siegfriedpammer

ok if you can point the right direction i possibly can add support for it or are you going to eventually go to add support

modz2014 avatar Jan 26 '22 04:01 modz2014

I think we can add support for this. Would you be willing to contribute it? I will post some sample code on how to extend the decompiler in the evening.

siegfriedpammer avatar Jan 26 '22 10:01 siegfriedpammer

yes possibly do you have discord so we can chat live

modz2014 avatar Jan 26 '22 10:01 modz2014

I don't think support for this is going to be easy -- effectively what needs to happen is that a WithEvents field needs to be replaced with a regular field, moving the event handler registrations into InitializeComponents. But this transform is only semantically correct iff the field is only assigned to within InitializeComponents -- but in VB, nothing stops the WithEvents field from being public and having assignments all over the place, even in other classes.

dgrunwald avatar Jan 26 '22 10:01 dgrunwald

yes i can see them when trying to decompile but it might take a while to get it sorted though i guess

modz2014 avatar Jan 26 '22 11:01 modz2014

effectively what needs to happen is that a WithEvents field needs to be replaced with a regular field

As far as I can tell, WithEvents fields are implemented as CLR properties, which are already transformed to C# auto-properties, if there are no event assignments. The only way user-code may access designer-generated controls is via the CLR properties.

@modz2014 So you would have to add a transform that analyzes the properties of all classes that are derived from System.Windows.Forms.Control and check if they follow this pattern:

public virtual Button Button1
{
	[CompilerGenerated]
	get
	{
		return _Button1;
	}
	[MethodImpl(MethodImplOptions.Synchronized)]
	[CompilerGenerated]
	set
	{
		EventHandler value2 = Button1_Click;
		Button button = _Button1;
		if (button != null)
		{
			button.Click -= value2; // event removal
		}
		_Button1 = value;
		button = _Button1;
		if (button != null)
		{
			button.Click += value2; // event add
		}
	}
}

If there are no event assignments, you will already get C# auto-properties, which can simply be replaced with fields. If there are events you will have to analyze the set-accessor of the property and move the event assignments to the InitializeComponent method.

While this can be done, I am sure it is impossible to cover all cases, because it is theoretically possible that a control is re-assigned outside of the InitializeComponent method and then the event assignments would not be adjusted.

Maybe you are better off doing manual adjustments. If you still want to implement a transform, you can take a look at https://github.com/icsharpcode/ILSpy/issues/2584#issuecomment-1001026037 for a general setup.

Also the code in https://github.com/icsharpcode/ILSpy/blob/master/ICSharpCode.Decompiler/CSharp/Transforms/PatternStatementTransform.cs#L613 will help you, because your transform will probably be very similar.

Hope this helps!

If you have questions, please feel free to ask them here.

siegfriedpammer avatar Jan 26 '22 18:01 siegfriedpammer

yer i have edited the form and added a designer form when decompiled from ILSpy in C# in took me 20mins to get the designer to show because of the project i was trying to do

ill give it and shot and let you guys know what happens

My computer stuff up trying yo add code to this project keep instilling and uninstalling laptop didn't like it

modz2014 avatar Jan 26 '22 19:01 modz2014

yer i had a look i dont think its possible the only way is to do it manually i think

modz2014 avatar Jan 27 '22 09:01 modz2014

i wonder if this will help https://github.com/telerik/justdecompile-plugins/blob/master/Reflexil.JustDecompile/reflexil.1.5.src/Libs/Sources/ICSharpCode.SharpDevelop.Dom/NRefactoryResolver/VBNetToCSharpConvertVisitor.cs

modz2014 avatar Jan 27 '22 13:01 modz2014