diff --git a/ServiceScan.SourceGenerator.Tests/CustomHandlerTests.cs b/ServiceScan.SourceGenerator.Tests/CustomHandlerTests.cs index f3ddb9d..17a94db 100644 --- a/ServiceScan.SourceGenerator.Tests/CustomHandlerTests.cs +++ b/ServiceScan.SourceGenerator.Tests/CustomHandlerTests.cs @@ -397,6 +397,53 @@ public static partial class ModelBuilderExtensions Assert.Equal(expected, results.GeneratedTrees[1].ToString()); } + [Fact] + public void UseInstanceCustomHandlerMethod() + { + var source = $$""" + using ServiceScan.SourceGenerator; + + namespace GeneratorTests; + + public partial class ServicesExtensions + { + [GenerateServiceRegistrations(AssignableTo = typeof(IService), CustomHandler = nameof(HandleType))] + public partial void ProcessServices(); + + private void HandleType() => System.Console.WriteLine(typeof(T).Name); + } + """; + + var services = + """ + namespace GeneratorTests; + + public interface IService { } + public class MyService1 : IService { } + public class MyService2 : IService { } + """; + + var compilation = CreateCompilation(source, services); + + var results = CSharpGeneratorDriver + .Create(_generator) + .RunGenerators(compilation) + .GetRunResult(); + + var expected = $$""" + namespace GeneratorTests; + + public partial class ServicesExtensions + { + public partial void ProcessServices() + { + HandleType(); + HandleType(); + } + } + """; + Assert.Equal(expected, results.GeneratedTrees[1].ToString()); + } private static Compilation CreateCompilation(params string[] source) { diff --git a/ServiceScan.SourceGenerator/DependencyInjectionGenerator.ParseMethodModel.cs b/ServiceScan.SourceGenerator/DependencyInjectionGenerator.ParseMethodModel.cs index dae59a4..3ef68de 100644 --- a/ServiceScan.SourceGenerator/DependencyInjectionGenerator.ParseMethodModel.cs +++ b/ServiceScan.SourceGenerator/DependencyInjectionGenerator.ParseMethodModel.cs @@ -54,7 +54,7 @@ public partial class DependencyInjectionGenerator if (attribute.CustomHandler != null) { var customHandlerMethod = method.ContainingType.GetMembers().OfType() - .FirstOrDefault(m => m.IsStatic && m.Name == attribute.CustomHandler); + .FirstOrDefault(m => m.Name == attribute.CustomHandler); if (customHandlerMethod is null) return Diagnostic.Create(CustomHandlerMethodNotFound, attribute.Location); diff --git a/ServiceScan.SourceGenerator/DiagnosticDescriptors.cs b/ServiceScan.SourceGenerator/DiagnosticDescriptors.cs index db73d00..fa41055 100644 --- a/ServiceScan.SourceGenerator/DiagnosticDescriptors.cs +++ b/ServiceScan.SourceGenerator/DiagnosticDescriptors.cs @@ -62,7 +62,7 @@ public static class DiagnosticDescriptors public static readonly DiagnosticDescriptor CustomHandlerMethodNotFound = new("DI0012", "Provided CustomHandler method is not found", - "CustomHandler parameter should point to a static method in the class", + "CustomHandler parameter should point to a method in the class", "Usage", DiagnosticSeverity.Error, true);