forked from dotnet/efcore
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMethodCallCodeFragmentExtensions.cs
39 lines (36 loc) · 1.27 KB
/
MethodCallCodeFragmentExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
namespace Microsoft.EntityFrameworkCore.Design;
/// <summary>
/// Design-time <see cref="MethodCallCodeFragment"/> extensions.
/// </summary>
public static class MethodCallCodeFragmentExtensions
{
/// <summary>
/// Gets the using statements required for this method call.
/// </summary>
/// <param name="methodCall">The method call.</param>
/// <returns>The usings.</returns>
public static IEnumerable<string> GetRequiredUsings(this MethodCallCodeFragment methodCall)
{
var method = methodCall.MethodInfo;
if (method?.IsStatic == true)
{
yield return method.DeclaringType!.Namespace!;
}
foreach (var argument in methodCall.Arguments)
{
if (argument is NestedClosureCodeFragment nestedClosure)
{
foreach (var nestedUsing in nestedClosure.MethodCalls.SelectMany(GetRequiredUsings))
{
yield return nestedUsing;
}
}
else if (argument is not null)
{
yield return argument.GetType().Namespace!;
}
}
}
}