Skip to content
New issue

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

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

Already on GitHub? # to your account

Add support for VB.NET automatic events #2845

Merged
merged 1 commit into from
Nov 25, 2022

Conversation

ElektroKill
Copy link
Contributor

Link to issue(s) this covers:
N/A

Problem

ILSpy did not properly support automatic events generated by the Visual Basic compiler. The backing field naming convention used by the Visual Basic compiler is different than the one used by the C# one. Instead of the field having the same name as the event, it uses the events name and appends the Event suffix to it. This different naming convention caused ILSpy to fail to detect automatic events and the decompiled code was very messy.

Before:

public class VBAutomaticEvents
{
	public delegate void EventWithParameterEventHandler(int EventNumber);

	public delegate void EventWithoutParameterEventHandler();

	[CompilerGenerated]
	private EventWithParameterEventHandler EventWithParameterEvent;

	[CompilerGenerated]
	private EventWithoutParameterEventHandler EventWithoutParameterEvent;

	public event EventWithParameterEventHandler EventWithParameter
	{
		[CompilerGenerated]
		add
		{
			EventWithParameterEventHandler eventWithParameterEventHandler = EventWithParameterEvent;
			EventWithParameterEventHandler eventWithParameterEventHandler2;
			do
			{
				eventWithParameterEventHandler2 = eventWithParameterEventHandler;
				EventWithParameterEventHandler value2 = (EventWithParameterEventHandler)Delegate.Combine(eventWithParameterEventHandler2, value);
				eventWithParameterEventHandler = Interlocked.CompareExchange(ref EventWithParameterEvent, value2, eventWithParameterEventHandler2);
			}
			while ((object)eventWithParameterEventHandler != eventWithParameterEventHandler2);
		}
		[CompilerGenerated]
		remove
		{
			EventWithParameterEventHandler eventWithParameterEventHandler = EventWithParameterEvent;
			EventWithParameterEventHandler eventWithParameterEventHandler2;
			do
			{
				eventWithParameterEventHandler2 = eventWithParameterEventHandler;
				EventWithParameterEventHandler value2 = (EventWithParameterEventHandler)Delegate.Remove(eventWithParameterEventHandler2, value);
				eventWithParameterEventHandler = Interlocked.CompareExchange(ref EventWithParameterEvent, value2, eventWithParameterEventHandler2);
			}
			while ((object)eventWithParameterEventHandler != eventWithParameterEventHandler2);
		}
	}

	public event EventWithoutParameterEventHandler EventWithoutParameter
	{
		[CompilerGenerated]
		add
		{
			EventWithoutParameterEventHandler eventWithoutParameterEventHandler = EventWithoutParameterEvent;
			EventWithoutParameterEventHandler eventWithoutParameterEventHandler2;
			do
			{
				eventWithoutParameterEventHandler2 = eventWithoutParameterEventHandler;
				EventWithoutParameterEventHandler value2 = (EventWithoutParameterEventHandler)Delegate.Combine(eventWithoutParameterEventHandler2, value);
				eventWithoutParameterEventHandler = Interlocked.CompareExchange(ref EventWithoutParameterEvent, value2, eventWithoutParameterEventHandler2);
			}
			while ((object)eventWithoutParameterEventHandler != eventWithoutParameterEventHandler2);
		}
		[CompilerGenerated]
		remove
		{
			EventWithoutParameterEventHandler eventWithoutParameterEventHandler = EventWithoutParameterEvent;
			EventWithoutParameterEventHandler eventWithoutParameterEventHandler2;
			do
			{
				eventWithoutParameterEventHandler2 = eventWithoutParameterEventHandler;
				EventWithoutParameterEventHandler value2 = (EventWithoutParameterEventHandler)Delegate.Remove(eventWithoutParameterEventHandler2, value);
				eventWithoutParameterEventHandler = Interlocked.CompareExchange(ref EventWithoutParameterEvent, value2, eventWithoutParameterEventHandler2);
			}
			while ((object)eventWithoutParameterEventHandler != eventWithoutParameterEventHandler2);
		}
	}

	public void RaiseEvents()
	{
		EventWithParameterEvent?.Invoke(1);
		EventWithoutParameterEvent?.Invoke();
	}
}

After:

public class VBAutomaticEvents
{
	public delegate void EventWithParameterEventHandler(int EventNumber);

	public delegate void EventWithoutParameterEventHandler();

	public event EventWithParameterEventHandler EventWithParameter;

	public event EventWithoutParameterEventHandler EventWithoutParameter;

	public void RaiseEvents()
	{
		EventWithParameter?.Invoke(1);
		EventWithoutParameter?.Invoke();
	}
}

Original VB code

Public Class VBAutomaticEvents
	Event EventWithParameter(ByVal EventNumber As Integer)
	Event EventWithoutParameter()

	Sub RaiseEvents()
		RaiseEvent EventWithParameter(1)
		RaiseEvent EventWithoutParameter()
	End Sub
End Class

Solution

I adapted the code for handling automatic events to support the naming convention used by the Visual Basic compiler.
A VBPretty test was added to test this newly added behavior.

@siegfriedpammer
Copy link
Member

Thank you very much for your contribution!

LGTM

@siegfriedpammer siegfriedpammer merged commit be1303f into icsharpcode:master Nov 25, 2022
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants