From be6efa3fe6275f5068b6f46117b2675bc5dc7fff Mon Sep 17 00:00:00 2001 From: romerod Date: Mon, 13 Dec 2021 07:34:36 +0100 Subject: [PATCH] Make WithMetadataFrom for open generics public --- ...enGenericScanningRegistrationExtensions.cs | 37 ------------------ ...nExtensions.OpenGenericAssemblyScanning.cs | 38 +++++++++++++++++++ 2 files changed, 38 insertions(+), 37 deletions(-) diff --git a/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs b/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs index e5d8a2dcb..afe48341b 100644 --- a/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs +++ b/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs @@ -250,42 +250,5 @@ public static IRegistrationBuilder candidateType.IsOpenGenericTypeOf(openGenericServiceType)) .As(candidateType => (Service)new KeyedService(serviceKeyMapping(candidateType), candidateType)); } - - /// - /// Use the properties of an attribute (or interface implemented by an attribute) on the scanned type - /// to provide metadata values. - /// - /// Inherited attributes are supported; however, there must be at most one matching attribute - /// in the inheritance chain. - /// The attribute applied to the scanned type. - /// Registration to set metadata on. - /// Registration builder allowing the registration to be configured. - public static IRegistrationBuilder - WithMetadataFrom( - this IRegistrationBuilder registration) - { - var attrType = typeof(TAttribute); - var metadataProperties = attrType - .GetRuntimeProperties() - .Where(pi => pi.CanRead); - - return registration.WithMetadata(t => - { - var attrs = t.GetCustomAttributes(true).OfType().ToList(); - - if (attrs.Count == 0) - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationExtensionsResources.MetadataAttributeNotFound, typeof(TAttribute), t)); - } - - if (attrs.Count != 1) - { - throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationExtensionsResources.MultipleMetadataAttributesSameType, typeof(TAttribute), t)); - } - - var attr = attrs[0]; - return metadataProperties.Select(p => new KeyValuePair(p.Name, p.GetValue(attr, null))); - }); - } } } diff --git a/src/Autofac/RegistrationExtensions.OpenGenericAssemblyScanning.cs b/src/Autofac/RegistrationExtensions.OpenGenericAssemblyScanning.cs index 918a1652c..38a2efdcb 100644 --- a/src/Autofac/RegistrationExtensions.OpenGenericAssemblyScanning.cs +++ b/src/Autofac/RegistrationExtensions.OpenGenericAssemblyScanning.cs @@ -4,6 +4,7 @@ using System; using System.Collections.Generic; using System.Diagnostics.CodeAnalysis; +using System.Globalization; using System.Linq; using System.Reflection; using Autofac.Builder; @@ -320,5 +321,42 @@ public static IRegistrationBuilder rb.WithMetadata(metadataMapping(t))); return registration; } + + /// + /// Use the properties of an attribute (or interface implemented by an attribute) on the scanned type + /// to provide metadata values. + /// + /// Inherited attributes are supported; however, there must be at most one matching attribute + /// in the inheritance chain. + /// The attribute applied to the scanned type. + /// Registration to set metadata on. + /// Registration builder allowing the registration to be configured. + public static IRegistrationBuilder + WithMetadataFrom( + this IRegistrationBuilder registration) + { + var attrType = typeof(TAttribute); + var metadataProperties = attrType + .GetRuntimeProperties() + .Where(pi => pi.CanRead); + + return registration.WithMetadata(t => + { + var attrs = t.GetCustomAttributes(true).OfType().ToList(); + + if (attrs.Count == 0) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationExtensionsResources.MetadataAttributeNotFound, typeof(TAttribute), t)); + } + + if (attrs.Count != 1) + { + throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, RegistrationExtensionsResources.MultipleMetadataAttributesSameType, typeof(TAttribute), t)); + } + + var attr = attrs[0]; + return metadataProperties.Select(p => new KeyValuePair(p.Name, p.GetValue(attr, null))); + }); + } } }