Skip to content

Commit

Permalink
Extracting code #2
Browse files Browse the repository at this point in the history
  • Loading branch information
Neme12 committed Mar 10, 2018
1 parent ae41119 commit 90c4bca
Showing 1 changed file with 22 additions and 20 deletions.
42 changes: 22 additions & 20 deletions src/Workspaces/Core/Portable/Shared/Extensions/ISymbolExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,43 +493,45 @@ public static ITypeSymbol ConvertToType(
var count = extensionUsedAsInstance ? Math.Max(0, method.Parameters.Length - 1) : method.Parameters.Length;
var skip = extensionUsedAsInstance ? 1 : 0;

// Convert the symbol to Func<...> or Action<...>
if (method.ReturnsVoid)
string WithArity(string typeName, int arity) => arity > 0 ? typeName + '`' + arity : typeName;

var delegateTypeName = method.ReturnsVoid
? WithArity("System.Action", count)
: WithArity("System.Func", count + 1);
var delegateType = compilation.GetTypeByMetadataName(delegateTypeName);

if (delegateType != null)
{
// Action<TArg1, ..., TArgN>
var actionName = "System.Action" + (count > 0 ? $"`{count}" : "");
var actionType = compilation.GetTypeByMetadataName(actionName);
ITypeSymbol[] types;

if (actionType != null)
// Convert the symbol to Func<...> or Action<...>
if (method.ReturnsVoid)
{
var types = method.Parameters
// Action<TArg1, ..., TArgN>

types = method.Parameters
.Skip(skip)
.Select(p =>
p.Type == null ?
compilation.GetSpecialType(SpecialType.System_Object) :
p.Type)
.ToArray();
return types.Length > 0 ? actionType.Construct(types) : actionType;
}
}
else
{
// Func<TArg1,...,TArgN,TReturn>
//
// +1 for the return type.
var functionName = "System.Func`" + (count + 1);
var functionType = compilation.GetTypeByMetadataName(functionName);

if (functionType != null)
else
{
var types = method.Parameters
// Func<TArg1,...,TArgN,TReturn>
//
// +1 for the return type.

types = method.Parameters
.Skip(skip)
.Select(p => p.Type)
.Concat(method.ReturnType)
.Select(t => t ?? compilation.GetSpecialType(SpecialType.System_Object))
.ToArray();
return functionType.Construct(types);
}

return types.Length > 0 ? delegateType.Construct(types) : delegateType;
}
}

Expand Down

0 comments on commit 90c4bca

Please sign in to comment.