From b61d3d74c19a7bf87c9aa99104f760b5dab36472 Mon Sep 17 00:00:00 2001 From: Colin Wilmans Date: Sat, 15 Oct 2022 20:13:36 +0200 Subject: [PATCH] Only generate the interface when it is accessible from the module (so public or it resides in the same module) --- src/SourceInject/Generator.cs | 10 ++++++++-- test/ConsoleApp/ExampleService.cs | 5 +++-- test/ConsoleApp/IExampleService.cs | 12 ++++++++++++ test/Lib/ILibInternalInterface.cs | 6 ++++++ test/Lib/ILibPublicInterface.cs | 12 ++++++++++++ test/Lib/LibBaseService.cs | 6 ++++++ 6 files changed, 47 insertions(+), 4 deletions(-) create mode 100644 test/ConsoleApp/IExampleService.cs create mode 100644 test/Lib/ILibInternalInterface.cs create mode 100644 test/Lib/ILibPublicInterface.cs create mode 100644 test/Lib/LibBaseService.cs diff --git a/src/SourceInject/Generator.cs b/src/SourceInject/Generator.cs index c7b911a..0f04b0c 100644 --- a/src/SourceInject/Generator.cs +++ b/src/SourceInject/Generator.cs @@ -69,9 +69,15 @@ public void Execute(GeneratorExecutionContext context) break; default: break; - } + } foreach (var interf in ((ITypeSymbol)symbol).AllInterfaces) - { + { + if (interf.DeclaredAccessibility != Accessibility.Public && + !SymbolEqualityComparer.Default.Equals(interf.ContainingModule, context.Compilation.SourceModule)) + { + continue; + } + switch (lifetime) { case Lifetime.Singleton: diff --git a/test/ConsoleApp/ExampleService.cs b/test/ConsoleApp/ExampleService.cs index 9b90cba..e8161fb 100644 --- a/test/ConsoleApp/ExampleService.cs +++ b/test/ConsoleApp/ExampleService.cs @@ -1,9 +1,10 @@ +using Lib; using Microsoft.Extensions.DependencyInjection; namespace ConsoleApp; [Inject] -public class ExampleService +public class ExampleService : LibBaseService, IExampleService { private readonly AnotherService anotherService; @@ -19,7 +20,7 @@ public interface IAnotherService } [Inject(ServiceLifetime.Singleton)] -public class AnotherService : IAnotherService +public class AnotherService : LibBaseService, IAnotherService { public string Value => "Hello World!"; } diff --git a/test/ConsoleApp/IExampleService.cs b/test/ConsoleApp/IExampleService.cs new file mode 100644 index 0000000..d52c6a7 --- /dev/null +++ b/test/ConsoleApp/IExampleService.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace ConsoleApp +{ + internal interface IExampleService + { + } +} diff --git a/test/Lib/ILibInternalInterface.cs b/test/Lib/ILibInternalInterface.cs new file mode 100644 index 0000000..92dfd21 --- /dev/null +++ b/test/Lib/ILibInternalInterface.cs @@ -0,0 +1,6 @@ +namespace Lib +{ + internal interface ILibInternalInterface + { + } +} diff --git a/test/Lib/ILibPublicInterface.cs b/test/Lib/ILibPublicInterface.cs new file mode 100644 index 0000000..17936d6 --- /dev/null +++ b/test/Lib/ILibPublicInterface.cs @@ -0,0 +1,12 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; + +namespace Lib +{ + public interface ILibPublicInterface + { + } +} diff --git a/test/Lib/LibBaseService.cs b/test/Lib/LibBaseService.cs new file mode 100644 index 0000000..427d8f6 --- /dev/null +++ b/test/Lib/LibBaseService.cs @@ -0,0 +1,6 @@ +namespace Lib +{ + public class LibBaseService : ILibInternalInterface, ILibPublicInterface + { + } +}