private static bool TryGetServiceAttribute(TypeInfo type, out ServiceAttribute serviceAttribute) { var customAttributeData = type.CustomAttributes.SingleOrDefault( e => ServiceAttributeType.IsAssignableFrom(e.AttributeType.GetTypeInfo())); if (customAttributeData == null) { serviceAttribute = null; return(false); } serviceAttribute = (ServiceAttribute) ServiceAttributeType.DeclaredConstructors.Single().Invoke( new[] { customAttributeData.ConstructorArguments[0].Value }); foreach (var namedArgument in customAttributeData.NamedArguments) { switch (namedArgument.MemberName) { case nameof(ServiceAttribute.Registration): { serviceAttribute.Registration = (RegistrationType)namedArgument.TypedValue.Value; break; } case nameof(ServiceAttribute.TryAdd): { serviceAttribute.TryAdd = (bool)namedArgument.TypedValue.Value; break; } case nameof(ServiceAttribute.TypesToRegister): { serviceAttribute.TypesToRegister = ((IEnumerable <CustomAttributeTypedArgument>) namedArgument.TypedValue.Value).Select(e => (Type)e.Value).ToArray(); break; } default: throw new InvalidOperationException( $"The member '{namedArgument.MemberName}' has no assign code"); } } return(true); }
private static bool TryGetServiceAttribute(Type type, out ServiceAttribute serviceAttribute) { serviceAttribute = type.GetCustomAttribute <ServiceAttribute>(true); return(serviceAttribute != null); }