当前位置: 首页>>代码示例>>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;未经允许,请勿转载。