Skip to content

Commit 6401d0d

Browse files
committed
update InterceptorSamples
1 parent b8acf8f commit 6401d0d

2 files changed

Lines changed: 9 additions & 18 deletions

File tree

InterceptorSamples/InterceptorGenerators/CreateScopeActivityGenerator.cs

Lines changed: 8 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,18 +14,11 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
1414
var methodCalls = context.SyntaxProvider.CreateSyntaxProvider(
1515
predicate: static (node, _) =>
1616
{
17-
if (node is
18-
InvocationExpressionSyntax
17+
if (node is InvocationExpressionSyntax
1918
{
2019
Expression: MemberAccessExpressionSyntax
2120
{
22-
Name:
23-
{
24-
Identifier:
25-
{
26-
ValueText: "CreateScope" or "CreateAsyncScope"
27-
}
28-
}
21+
Name.Identifier.ValueText: "CreateScope" or "CreateAsyncScope"
2922
}
3023
})
3124
{
@@ -67,9 +60,12 @@ public void Initialize(IncrementalGeneratorInitializationContext context)
6760
var interceptorCode =
6861
(invocationGroup.Key.ContainingTypeName, invocationGroup.Key.MethodName) switch
6962
{
70-
("Microsoft.Extensions.DependencyInjection.IServiceScopeFactory", "CreateScope") => ScopeActivityGeneratedSource.ServiceScopeFactoryCreateScopeInterceptorCode,
71-
("Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions", "CreateScope") => ScopeActivityGeneratedSource.ServiceProviderCreateScopeInterceptorCode,
72-
("Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions", "CreateAsyncScope") => ScopeActivityGeneratedSource.ServiceProviderCreateScopeAsyncInterceptorCode,
63+
("Microsoft.Extensions.DependencyInjection.IServiceScopeFactory", "CreateScope")
64+
=> ScopeActivityGeneratedSource.ServiceScopeFactoryCreateScopeInterceptorCode,
65+
("Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions", "CreateScope")
66+
=> ScopeActivityGeneratedSource.ServiceProviderCreateScopeInterceptorCode,
67+
("Microsoft.Extensions.DependencyInjection.ServiceProviderServiceExtensions", "CreateAsyncScope")
68+
=> ScopeActivityGeneratedSource.ServiceProviderCreateScopeAsyncInterceptorCode,
7369
_ => throw new ArgumentOutOfRangeException($"{invocationGroup.Key.MethodName}")
7470
};
7571
stringBuilder.AppendLine(interceptorCode);

InterceptorSamples/InterceptorGenerators/InterceptInvocation.cs

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,14 @@ namespace InterceptorGenerators;
77
internal sealed class InterceptInvocation
88
{
99
private readonly MemberAccessExpressionSyntax _memberAccessExpressionSyntax;
10-
1110
private readonly IInvocationOperation _invocationOperation;
1211

1312
public InterceptInvocation(IInvocationOperation invocationOperation)
1413
{
1514
_invocationOperation = invocationOperation;
16-
// The invocation expression consists of two properties:
17-
// - Expression: which is a `MemberAccessExpressionSyntax` that represents the method being invoked.
18-
// - ArgumentList: the list of arguments being invoked.
19-
// Here, we resolve the `MemberAccessExpressionSyntax` to get the location of the method being invoked.
2015
_memberAccessExpressionSyntax =
2116
(MemberAccessExpressionSyntax)((InvocationExpressionSyntax)_invocationOperation.Syntax)
2217
.Expression;
23-
// `MemberAccessExpressionSyntax`.Name: the name of the member being accessed
2418
MethodName = _memberAccessExpressionSyntax.Name.Identifier.Text;
2519
AssemblyName = _invocationOperation.TargetMethod.ContainingAssembly.MetadataName;
2620
ContainingNamespace = _invocationOperation.TargetMethod.ContainingNamespace.GetFullNamespace();
@@ -48,6 +42,7 @@ public InterceptInvocation(IInvocationOperation invocationOperation)
4842

4943
private (string filePath, int line, int column) GetLocation()
5044
{
45+
// https://github.com/dotnet/aspnetcore/blob/3f1acb59718cadf111a0a796681e3d3509bb3381/src/Http/Http.Extensions/gen/StaticRouteHandlerModel/Endpoint.cs#L143
5146
// The `MemberAccessExpressionSyntax` in turn includes three properties:
5247
// - Expression: the expression that is being accessed.
5348
// - OperatorToken: the operator token, typically the dot separate.

0 commit comments

Comments
 (0)