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

foreach expected but while used instead #1603

Open
greenozon opened this issue Jul 29, 2019 · 3 comments
Open

foreach expected but while used instead #1603

greenozon opened this issue Jul 29, 2019 · 3 comments
Labels
C# Decompiler The decompiler engine itself Enhancement Areas for improvement Help Wanted

Comments

@greenozon
Copy link

greenozon commented Jul 29, 2019

ILSpy version 5.0.0.4970-preview3

method decompiled with while but foreach expected

protected CompositeDevices(SerializationInfo info, StreamingContext context)
	: base(info, context)
{
	SerializationInfoEnumerator enumerator = info.GetEnumerator();
	while (enumerator.MoveNext())
	{
		string name = enumerator.Current.Name;
		if (name != null && name == "AdditionalParameters")
		{
			AdditionalParameters = (Dictionary<string, string>)info.GetValue("AdditionalParameters", typeof(Dictionary<string, string>));
		}
	}
	base.CollectionChanged += OnCollectionChanged;
}

please also ref to very similar method
CompositeDevices(IEnumerable col)
that decompiled to foreach!

target: #1601

PS this assemlby has a lot of other locations that are decompiled to while loop instead of much more expected foreach!

@dgrunwald
Copy link
Member

SerializationInfoEnumerator implements IEnumerator but not IDisposable, so the compiler doesn't generate a using statement / try-finally block.
I think currently our foreach pattern only tries to match using statements.

@greenozon
Copy link
Author

Did a little investigation - how competitors are handling this case
so,
.NET Reflector as well as Telerik are using

while (enumerator.MoveNext())

but JetBrains and dnSpy are using

foreach (SerializationEntry serializationEntry in info)

so, to summarize

  1. it's possilbe
  2. it's "nice to have & see" feature, not an error

Feel free to close this issue.

@siegfriedpammer
Copy link
Member

See the relevant test case at

// TODO : Needs additional pattern detection
// CustomStructEnumerator does not implement IDisposable
// No try-finally-Dispose is generated.
//public void ForEachOnGenericCustomStructEnumerator<T>(CustomStructEnumerator<T> e)
//{
// foreach (T item in e) {
// Console.WriteLine(item);
// }
//}

@siegfriedpammer siegfriedpammer added C# Decompiler The decompiler engine itself Enhancement Areas for improvement Help Wanted labels Jul 29, 2019
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