コード例 #1
0
ファイル: AttributeSelector.cs プロジェクト: leiwcq/EasyH
        void ISelector.Populate(IServiceCollection services)
        {
            if (services == null)
            {
                throw new ArgumentNullException(nameof(services));
            }

            foreach (var type in Types)
            {
                var typeInfo = type.GetTypeInfo();

                var attributes = typeInfo.GetCustomAttributes <ServiceDescriptorAttribute>().ToArray();

                // Check if the type has multiple attributes with same ServiceType.
                var duplicates = attributes
                                 .GroupBy(s => s.ServiceType)
                                 .SelectMany(grp => grp.Skip(1));

                if (duplicates.Any())
                {
                    throw new InvalidOperationException($@"Type ""{type.FullName}"" has multiple ServiceDescriptor attributes with the same service type.");
                }

                foreach (var attribute in attributes)
                {
                    var serviceTypes = GetServiceTypes(type, attribute);

                    foreach (var serviceType in serviceTypes)
                    {
                        var descriptor = new ServiceDescriptor(serviceType, type, attribute.Lifetime);

                        services.Add(descriptor);

                        if (serviceType.IsInterface)
                        {
                            var extensionDescriptor = (ServiceDescriptor)ServiceTypeMetadataExtensions
                                                      .GetMetadataServiceDescriptorMethodInfo
                                                      .MakeGenericMethod(descriptor.ServiceType)
                                                      .FastInvoke(null, new object[] { descriptor.ImplementationType, null });

                            services.Add(extensionDescriptor);

                            ServiceTypeMetadataExtensions.AddMetadata(descriptor.ImplementationType, attribute.Name);
                        }
                    }
                }
            }
        }
コード例 #2
0
        public static IServiceCollection RegisterInstance <TService>(this IServiceCollection services, object instance, string name)
        {
            var serviceType = typeof(TService);

            var descriptor = new ServiceDescriptor(typeof(TService), instance);

            services.Add(descriptor);


            var extensionDescriptor = (ServiceDescriptor)ServiceTypeMetadataExtensions
                                      .GetMetadataServiceDescriptorMethodInfo
                                      .MakeGenericMethod(descriptor.ServiceType)
                                      .FastInvoke(null, new object[] { instance.GetType(), instance });

            services.Add(extensionDescriptor);

            int serviceKey = instance.GetHashCode();

            ServiceTypeMetadataExtensions.AddMetadata(instance.GetType(), serviceKey, name);



            return(services);
        }