Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions ICSharpCode.Decompiler.Tests/TestCases/Pretty/AnonymousTypes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -174,5 +174,18 @@ private static Action<T> MakeAction<T>(T template)
{
return null;
}
#if ROSLYN
private static TValue GetOrCreate<TKey, TValue>(TKey key, Func<TKey, int, TValue> factory)
{
return factory(key, 1);
}

private static int MixedLambdaParameters()
{
return GetOrCreate(new {
Value = 1
}, (item, context) => item.Value + context);
}
#endif
}
}
17 changes: 15 additions & 2 deletions ICSharpCode.Decompiler/CSharp/ExpressionBuilder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2669,6 +2669,8 @@ IType GetTaskType(IType resultType)
IEnumerable<ParameterDeclaration> MakeParameters(IReadOnlyList<IParameter> parameters, ILFunction function)
{
var variables = function.Variables.Where(v => v.Kind == VariableKind.Parameter).ToDictionary(v => v.Index!.Value);
var result = new List<ParameterDeclaration>(parameters.Count);
bool anyAnonymousType = false;
int i = 0;
foreach (var parameter in parameters)
{
Expand All @@ -2684,10 +2686,21 @@ IEnumerable<ParameterDeclaration> MakeParameters(IReadOnlyList<IParameter> param
pd.Name = "P_" + i;
}
if (settings.AnonymousTypes && parameter.Type.ContainsAnonymousType())
pd.Type = null;
yield return pd;
anyAnonymousType = true;
result.Add(pd);
i++;
}
// An anonymous type cannot be named, so a lambda with such a parameter must be implicitly typed.
// C# also requires all lambda parameters to use the same form (CS0748). Drop every type when the
// remaining parameter syntax permits it; otherwise keep the converted declarations as a
// best-effort fallback for an unrepresentable signature.
if (anyAnonymousType && result.All(p => p.ParameterModifier == ReferenceKind.None
&& !p.IsParams && !p.IsScopedRef && p.Attributes.Count == 0 && p.DefaultExpression is null))
{
foreach (var pd in result)
pd.Type = null;
}
return result;
}

protected internal override TranslatedExpression VisitBlockContainer(BlockContainer container, TranslationContext context)
Expand Down
Loading