From 0e683f603c8b7f59e878e63109716d260f32f644 Mon Sep 17 00:00:00 2001 From: maliming Date: Mon, 28 Mar 2022 13:35:17 +0800 Subject: [PATCH] Support for exporting generic interfaces. --- .../ExposeServicesAttribute.cs | 6 ++++- .../AutoRegistrationHelper_Tests.cs | 25 +++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposeServicesAttribute.cs b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposeServicesAttribute.cs index 72e7870125..286b9a8c76 100644 --- a/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposeServicesAttribute.cs +++ b/framework/src/Volo.Abp.Core/Volo/Abp/DependencyInjection/ExposeServicesAttribute.cs @@ -15,7 +15,7 @@ public class ExposeServicesAttribute : Attribute, IExposedServiceTypesProvider public ExposeServicesAttribute(params Type[] serviceTypes) { - ServiceTypes = serviceTypes ?? new Type[0]; + ServiceTypes = serviceTypes ?? Type.EmptyTypes; } public Type[] GetExposedServiceTypes(Type targetType) @@ -49,6 +49,10 @@ public class ExposeServicesAttribute : Attribute, IExposedServiceTypesProvider foreach (var interfaceType in type.GetTypeInfo().GetInterfaces()) { var interfaceName = interfaceType.Name; + if (interfaceType.IsGenericType) + { + interfaceName = interfaceType.Name.Left(interfaceType.Name.IndexOf('`')); + } if (interfaceName.StartsWith("I")) { diff --git a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoRegistrationHelper_Tests.cs b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoRegistrationHelper_Tests.cs index 3c7ccfbc6a..5d8716a70c 100644 --- a/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoRegistrationHelper_Tests.cs +++ b/framework/test/Volo.Abp.Core.Tests/Volo/Abp/DependencyInjection/AutoRegistrationHelper_Tests.cs @@ -33,6 +33,21 @@ public class AutoRegistrationHelper_Tests exposedServices.ShouldContain(typeof(IDerivedService)); } + [Fact] + public void Should_Get_Conventional_Exposed_Generic_Types_By_Default() + { + //Act + var exposedServices = ExposedServiceExplorer.GetExposedServices(typeof(DefaultGenericService)); + + //Assert + exposedServices.Count.ShouldBe(4); + exposedServices.ShouldContain(typeof(IService)); + exposedServices.ShouldContain(typeof(IGenericService)); + exposedServices.ShouldContain(typeof(IGenericService)); + exposedServices.ShouldContain(typeof(DefaultGenericService)); + } + + public class DefaultDerivedService : IDerivedService { } @@ -50,4 +65,14 @@ public class AutoRegistrationHelper_Tests { } + + public interface IGenericService + { + + } + + public class DefaultGenericService : IService, IGenericService, IGenericService + { + + } }