Skip to content

Commit 87cc70e

Browse files
committed
Factoring out common code #3
1 parent 90c4bca commit 87cc70e

File tree

2 files changed

+12
-28
lines changed

2 files changed

+12
-28
lines changed

src/Workspaces/Core/Portable/Shared/Extensions/INamedTypeSymbolExtensions.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -531,5 +531,8 @@ private static void RemoveOverriddenMembers(
531531
}
532532
}
533533
}
534+
535+
public static INamedTypeSymbol TryConstruct(this INamedTypeSymbol type, ITypeSymbol[] typeArguments)
536+
=> typeArguments.Length > 0 ? type.Construct(typeArguments) : type;
534537
}
535538
}

src/Workspaces/Core/Portable/Shared/Extensions/ISymbolExtensions.cs

Lines changed: 9 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -495,43 +495,24 @@ public static ITypeSymbol ConvertToType(
495495

496496
string WithArity(string typeName, int arity) => arity > 0 ? typeName + '`' + arity : typeName;
497497

498-
var delegateTypeName = method.ReturnsVoid
498+
// Convert the symbol to Func<...> or Action<...>
499+
var delegateType = compilation.GetTypeByMetadataName(method.ReturnsVoid
499500
? WithArity("System.Action", count)
500-
: WithArity("System.Func", count + 1);
501-
var delegateType = compilation.GetTypeByMetadataName(delegateTypeName);
501+
: WithArity("System.Func", count + 1));
502502

503503
if (delegateType != null)
504504
{
505-
ITypeSymbol[] types;
505+
var types = method.Parameters
506+
.Skip(skip)
507+
.Select(p => p.Type ?? compilation.GetSpecialType(SpecialType.System_Object));
506508

507-
// Convert the symbol to Func<...> or Action<...>
508-
if (method.ReturnsVoid)
509+
if (!method.ReturnsVoid)
509510
{
510-
// Action<TArg1, ..., TArgN>
511-
512-
types = method.Parameters
513-
.Skip(skip)
514-
.Select(p =>
515-
p.Type == null ?
516-
compilation.GetSpecialType(SpecialType.System_Object) :
517-
p.Type)
518-
.ToArray();
519-
}
520-
else
521-
{
522-
// Func<TArg1,...,TArgN,TReturn>
523-
//
524511
// +1 for the return type.
525-
526-
types = method.Parameters
527-
.Skip(skip)
528-
.Select(p => p.Type)
529-
.Concat(method.ReturnType)
530-
.Select(t => t ?? compilation.GetSpecialType(SpecialType.System_Object))
531-
.ToArray();
512+
types = types.Concat(method.ReturnType ?? compilation.GetSpecialType(SpecialType.System_Object));
532513
}
533514

534-
return types.Length > 0 ? delegateType.Construct(types) : delegateType;
515+
return delegateType.TryConstruct(types.ToArray());
535516
}
536517
}
537518

0 commit comments

Comments
 (0)