當前位置: 首頁>>代碼示例>>C#>>正文


C# Container.GetCurrentRegistrations方法代碼示例

本文整理匯總了C#中SimpleInjector.Container.GetCurrentRegistrations方法的典型用法代碼示例。如果您正苦於以下問題:C# Container.GetCurrentRegistrations方法的具體用法?C# Container.GetCurrentRegistrations怎麽用?C# Container.GetCurrentRegistrations使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在SimpleInjector.Container的用法示例。


在下文中一共展示了Container.GetCurrentRegistrations方法的3個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。

示例1: FindAssignableRegistrations

        private static InstanceProducer[] FindAssignableRegistrations(Container container, 
            Type serviceType)
        {
            Type serviceTypeDefinition = serviceType.GetGenericTypeDefinition();

            return (
                from reg in container.GetCurrentRegistrations()
                where reg.ServiceType.IsGenericType
                where reg.ServiceType.GetGenericTypeDefinition() == serviceTypeDefinition
                where serviceType.IsAssignableFrom(reg.ServiceType)
                select reg)
                .ToArray();
        }
開發者ID:khellang,項目名稱:SimpleInjector,代碼行數:13,代碼來源:VarianceExtensions.cs

示例2: AllowToResolveVariantCollections

        public static void AllowToResolveVariantCollections(Container container)
        {
            container.ResolveUnregisteredType += (sender, e) =>
            {
                // Only handle IEnumerable<T>.
                if (!e.UnregisteredServiceType.IsGenericType ||
                    e.UnregisteredServiceType.GetGenericTypeDefinition() !=
                        typeof(IEnumerable<>))
                {
                    return;
                }

                Type serviceType =
                    e.UnregisteredServiceType.GetGenericArguments()[0];

                if (!serviceType.IsGenericType)
                {
                    return;
                }

                Type def = serviceType.GetGenericTypeDefinition();

                var registrations = (
                    from r in container.GetCurrentRegistrations()
                    where r.ServiceType.IsGenericType
                    where r.ServiceType.GetGenericTypeDefinition() == def
                    where serviceType.IsAssignableFrom(r.ServiceType)
                    select r)
                    .ToArray();

                if (registrations.Any())
                {
                    var instances =
                        registrations.Select(r => r.GetInstance());

                    var castMethod = typeof(Enumerable).GetMethod("Cast")
                        .MakeGenericMethod(serviceType);

                    var castedInstances =
                        castMethod.Invoke(null, new[] { instances });

                    e.Register(() => castedInstances);
                }
            };
        }
開發者ID:jbogard,項目名稱:GenericVarianceContainerTests,代碼行數:45,代碼來源:SimpleInjectorTests.cs

示例3: GetTypes

        private static IList<Type> GetTypes(Container container, Assembly assembly, Type[] typesToIgnore)
        {
            string assemblyName = assembly.GetName().Name;

            var existingRegistrationsServiceTypes =
                container.GetCurrentRegistrations().Select(instanceProducer => instanceProducer.ServiceType).ToList();

            IList<Type> registrations =
                assembly.GetExportedTypes()
                    .Where(type => !typesToIgnore.Contains(type))
                    .Where(type => !existingRegistrationsServiceTypes.Contains(type))
                    .Where(type => type.Namespace != null)
                    .Where(type => type.Namespace.StartsWith(assemblyName, StringComparison.OrdinalIgnoreCase))
                    .Where(type => (type.Attributes & TypeAttributes.Abstract) != TypeAttributes.Abstract)
                    .Where(type => type.GetInterfaces().Any())
                    .Where(
                        type =>
                            type.GetInterfaces()
                                .Any(
                                    inter =>
                                        !typesToIgnore.Contains(inter) && inter.Namespace != null &&
                                        inter.Namespace.StartsWith(assemblyName, StringComparison.OrdinalIgnoreCase)))

                    .ToList();

            // Ignore already registerd interfaces:
            for (int i = registrations.Count() - 1; i >= 0; i--)
            {
                foreach (var registrationInterface in registrations[i].GetInterfaces())
                {
                    if (existingRegistrationsServiceTypes.Contains(registrationInterface))
                    {
                        registrations.RemoveAt(i);
                        break;
                    }
                }
            }
            return registrations;
        }
開發者ID:jeroenpot,項目名稱:SimpleInjector-AssemblyScanner,代碼行數:39,代碼來源:DependencyRegistration.cs


注:本文中的SimpleInjector.Container.GetCurrentRegistrations方法示例由純淨天空整理自Github/MSDocs等開源代碼及文檔管理平台,相關代碼片段篩選自各路編程大神貢獻的開源項目,源碼版權歸原作者所有,傳播和使用請參考對應項目的License;未經允許,請勿轉載。