Skip to content

Commit

Permalink
Factoring out common code #3
Browse files Browse the repository at this point in the history
  • Loading branch information
Neme12 committed Mar 10, 2018
1 parent 90c4bca commit 87cc70e
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -531,5 +531,8 @@ private static void RemoveOverriddenMembers(
}
}
}

public static INamedTypeSymbol TryConstruct(this INamedTypeSymbol type, ITypeSymbol[] typeArguments)
=> typeArguments.Length > 0 ? type.Construct(typeArguments) : type;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -495,43 +495,24 @@ public static ITypeSymbol ConvertToType(

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

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

if (delegateType != null)
{
ITypeSymbol[] types;
var types = method.Parameters
.Skip(skip)
.Select(p => p.Type ?? compilation.GetSpecialType(SpecialType.System_Object));

// Convert the symbol to Func<...> or Action<...>
if (method.ReturnsVoid)
if (!method.ReturnsVoid)
{
// Action<TArg1, ..., TArgN>

types = method.Parameters
.Skip(skip)
.Select(p =>
p.Type == null ?
compilation.GetSpecialType(SpecialType.System_Object) :
p.Type)
.ToArray();
}
else
{
// 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();
types = types.Concat(method.ReturnType ?? compilation.GetSpecialType(SpecialType.System_Object));
}

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

Expand Down

0 comments on commit 87cc70e

Please sign in to comment.