Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
modz2014 opened this issue Jan 7, 2022 · 15 comments
Labels
C# Decompiler The decompiler engine itself Enhancement Areas for improvement Help Wanted

Comments

@modz2014
Copy link

modz2014 commented Jan 7, 2022

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

@siegfriedpammer
Copy link
Member

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: siegfried.pammer@gmail.com 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 siegfriedpammer added C# Decompiler The decompiler engine itself labels Jan 24, 2022
@siegfriedpammer siegfriedpammer changed the title Doesnt Decompile Visual Studio WinForms designer unable to display exported forms Jan 24, 2022
@modz2014
Copy link
Author

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

@siegfriedpammer
Copy link
Member

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!

@modz2014
Copy link
Author

Capture

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

@modz2014
Copy link
Author

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

@siegfriedpammer
Copy link
Member

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 siegfriedpammer changed the title Visual Studio WinForms designer unable to display exported forms Visual Studio WinForms designer unable to display forms exported from VB.NET assembly Jan 25, 2022
@modz2014
Copy link
Author

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

@siegfriedpammer siegfriedpammer added the Enhancement Areas for improvement label Jan 26, 2022
@siegfriedpammer
Copy link
Member

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.

@modz2014
Copy link
Author

yes possibly do you have discord so we can chat live

@dgrunwald
Copy link
Member

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.

@modz2014
Copy link
Author

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

@siegfriedpammer
Copy link
Member

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 #2584 (comment) 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.

@modz2014
Copy link
Author

modz2014 commented Jan 26, 2022

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
Copy link
Author

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C# Decompiler The decompiler engine itself Enhancement Areas for improvement Help Wanted
Projects
None yet
Development

No branches or pull requests

3 participants