diff --git a/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs b/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs index 4060283a5..afe48341b 100644 --- a/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs +++ b/src/Autofac/Features/Scanning/OpenGenericScanningRegistrationExtensions.cs @@ -250,59 +250,5 @@ public static IRegistrationBuilder candidateType.IsOpenGenericTypeOf(openGenericServiceType)) .As(candidateType => (Service)new KeyedService(serviceKeyMapping(candidateType), candidateType)); } - - /// - /// Specify how an open generic type from a scanned assembly provides metadata. - /// - /// Registration limit type. - /// Registration style. - /// Registration to set metadata on. - /// A function mapping the type to a list of metadata items. - /// Registration builder allowing the registration to be configured. - public static IRegistrationBuilder - WithMetadata( - this IRegistrationBuilder registration, - Func>> metadataMapping) - { - registration.ActivatorData.ConfigurationActions.Add((t, rb) => 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))); - }); - } } } diff --git a/src/Autofac/RegistrationExtensions.OpenGenericAssemblyScanning.cs b/src/Autofac/RegistrationExtensions.OpenGenericAssemblyScanning.cs index 947f3ff51..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; @@ -298,5 +299,64 @@ public static IRegistrationBuilder t.GetOpenGenericImplementedInterfaces()); } + + /// + /// Specify how a type from a scanned assembly provides metadata. + /// + /// Registration limit type. + /// Registration style. + /// Registration to set metadata on. + /// A function mapping the type to a list of metadata items. + /// Registration builder allowing the registration to be configured. + public static IRegistrationBuilder + WithMetadata( + this IRegistrationBuilder registration, + Func>> metadataMapping) + { + if (registration == null) + { + throw new ArgumentNullException(nameof(registration)); + } + + registration.ActivatorData.ConfigurationActions.Add((t, rb) => 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))); + }); + } } }