Skip to content

Commit 832a415

Browse files
authored
fix: Allow instance CustomHandler method if scan method is instance (#40)
1 parent 7fd4b30 commit 832a415

File tree

3 files changed

+49
-2
lines changed

3 files changed

+49
-2
lines changed

ServiceScan.SourceGenerator.Tests/CustomHandlerTests.cs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -397,6 +397,53 @@ public static partial class ModelBuilderExtensions
397397
Assert.Equal(expected, results.GeneratedTrees[1].ToString());
398398
}
399399

400+
[Fact]
401+
public void UseInstanceCustomHandlerMethod()
402+
{
403+
var source = $$"""
404+
using ServiceScan.SourceGenerator;
405+
406+
namespace GeneratorTests;
407+
408+
public partial class ServicesExtensions
409+
{
410+
[GenerateServiceRegistrations(AssignableTo = typeof(IService), CustomHandler = nameof(HandleType))]
411+
public partial void ProcessServices();
412+
413+
private void HandleType<T>() => System.Console.WriteLine(typeof(T).Name);
414+
}
415+
""";
416+
417+
var services =
418+
"""
419+
namespace GeneratorTests;
420+
421+
public interface IService { }
422+
public class MyService1 : IService { }
423+
public class MyService2 : IService { }
424+
""";
425+
426+
var compilation = CreateCompilation(source, services);
427+
428+
var results = CSharpGeneratorDriver
429+
.Create(_generator)
430+
.RunGenerators(compilation)
431+
.GetRunResult();
432+
433+
var expected = $$"""
434+
namespace GeneratorTests;
435+
436+
public partial class ServicesExtensions
437+
{
438+
public partial void ProcessServices()
439+
{
440+
HandleType<global::GeneratorTests.MyService1>();
441+
HandleType<global::GeneratorTests.MyService2>();
442+
}
443+
}
444+
""";
445+
Assert.Equal(expected, results.GeneratedTrees[1].ToString());
446+
}
400447

401448
private static Compilation CreateCompilation(params string[] source)
402449
{

ServiceScan.SourceGenerator/DependencyInjectionGenerator.ParseMethodModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public partial class DependencyInjectionGenerator
5454
if (attribute.CustomHandler != null)
5555
{
5656
var customHandlerMethod = method.ContainingType.GetMembers().OfType<IMethodSymbol>()
57-
.FirstOrDefault(m => m.IsStatic && m.Name == attribute.CustomHandler);
57+
.FirstOrDefault(m => m.Name == attribute.CustomHandler);
5858

5959
if (customHandlerMethod is null)
6060
return Diagnostic.Create(CustomHandlerMethodNotFound, attribute.Location);

ServiceScan.SourceGenerator/DiagnosticDescriptors.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public static class DiagnosticDescriptors
6262

6363
public static readonly DiagnosticDescriptor CustomHandlerMethodNotFound = new("DI0012",
6464
"Provided CustomHandler method is not found",
65-
"CustomHandler parameter should point to a static method in the class",
65+
"CustomHandler parameter should point to a method in the class",
6666
"Usage",
6767
DiagnosticSeverity.Error,
6868
true);

0 commit comments

Comments
 (0)