当前位置: 首页>>代码示例>>C#>>正文


C# IComponentContext.IsRegistered方法代码示例

本文整理汇总了C#中IComponentContext.IsRegistered方法的典型用法代码示例。如果您正苦于以下问题:C# IComponentContext.IsRegistered方法的具体用法?C# IComponentContext.IsRegistered怎么用?C# IComponentContext.IsRegistered使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在IComponentContext的用法示例。


在下文中一共展示了IComponentContext.IsRegistered方法的8个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: InjectProperties

        public void InjectProperties(IComponentContext context, object instance, bool overrideSetValues)
        {
            if (context == null) throw new ArgumentNullException("context");
            if (instance == null) throw new ArgumentNullException("instance");

            var instanceType = instance.GetType();

            foreach (var property in instanceType
                .GetProperties(BindingFlags.Public | BindingFlags.Instance)
                .Where(pi => pi.CanWrite))
            {
                var propertyType = property.PropertyType;

                if (propertyType.IsValueType && !propertyType.IsEnum)
                    continue;

                if (property.GetIndexParameters().Length != 0)
                    continue;

                if (!context.IsRegistered(propertyType))
                    continue;

                var accessors = property.GetAccessors(false);
                if (accessors.Length == 1 && accessors[0].ReturnType != typeof(void))
                    continue;

                if (!overrideSetValues &&
                    accessors.Length == 2 &&
                    (property.GetValue(instance, null) != null))
                    continue;

                var propertyValue = context.Resolve(propertyType);
                property.SetValue(instance, propertyValue, null);
            }
        }
开发者ID:GAlexandreBastien,项目名称:Dynamite-2010,代码行数:35,代码来源:AutofacBackportAutowiringPropertyInjector.cs

示例2: InjectProperties

        public static void InjectProperties(IComponentContext context, object instance)
        {
            var properties = instance.GetType().GetFields(
                BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic);

            foreach (var fieldInfo in properties)
            {
                var propertyType = fieldInfo.FieldType;
                if (propertyType.IsValueType || !context.IsRegistered(propertyType))
                {
                    continue;
                }

                if (HasImportAttribute(fieldInfo))
                {
                    if (fieldInfo.GetValue(instance) != null)
                    {
                        continue; // do not overwrite existing non-null values
                    }

                    var obj = context.Resolve(propertyType);
                    if (obj == null)
                    {
                        throw new DependencyResolutionException(
                            $"Unable to resolve dependency import on {instance.GetType()} -> {fieldInfo}");
                    }

                    fieldInfo.SetValue(instance, obj);
                }
            }
        }
开发者ID:clbond,项目名称:continuous-runner,代码行数:31,代码来源:PropertyInjector.cs

示例3: InjectProperties

		public void InjectProperties(IComponentContext context, object instance, bool overrideSetValues)
		{
			if (context == null) throw new ArgumentNullException("context");
			if (instance == null) throw new ArgumentNullException("instance");

			var instanceType = instance.GetType();

			foreach (var property in instanceType.GetProperties(
				BindingFlags.Public | BindingFlags.Instance | BindingFlags.SetProperty))
			{
				var propertyType = property.PropertyType;

				if (propertyType.IsValueType && !propertyType.IsEnum)
					continue;

				if (property.GetIndexParameters().Length != 0)
					continue;

				if (!context.IsRegistered(propertyType))
					continue;

				var accessors = property.GetAccessors(false);
				if (accessors.Length == 1 && accessors[0].ReturnType != typeof(void))
					continue;

				if (!overrideSetValues &&
					accessors.Length == 2 &&
					(property.GetValue(instance, null) != null))
					continue;

				IComponentRegistration registration;
				var service = new TypedService(propertyType);
				if (!context.ComponentRegistry.TryGetRegistration(service, out registration))
					throw new ComponentNotRegisteredException(service);

				var lookup = context.ResolveLookup(service, registration, Enumerable.Empty<Parameter>());
				try
				{
					if (lookup.Preparing)
						lookup.SharedInstanceActivation += (s, ea) => property.SetValue(instance, s, null);
					else
					{
						var propertyValue = lookup.Factory();
						property.SetValue(instance, propertyValue, null);
					}
				}
				catch (DependencyResolutionException dre)
				{
					dre.Lookups.Push(lookup);
					throw;
				}
			}
		}
开发者ID:dstimac,项目名称:revenj,代码行数:53,代码来源:AutowiringPropertyInjector.cs

示例4: UseMiddlewareFromContainer

        static void UseMiddlewareFromContainer(this IAppBuilder app, IComponentContext container)
        {
            var services = container.ComponentRegistry.Registrations.SelectMany(r => r.Services)
                .OfType<TypedService>()
                .Where(s => s.ServiceType.IsAssignableTo<OwinMiddleware>() && !s.ServiceType.IsAbstract)
                .Select(service => typeof(AutofacMiddleware<>).MakeGenericType(service.ServiceType))
                .Where(serviceType => !container.IsRegistered(serviceType));

            var typedServices = services.ToArray();
            if (!typedServices.Any()) return;

            foreach (var typedService in typedServices)
                app.Use(typedService);
        }
开发者ID:srogovtsev,项目名称:Autofac.Owin,代码行数:14,代码来源:AutofacAppBuilderExtensions.cs

示例5: InjectProperties

        public static void InjectProperties(IComponentContext context, object instance, bool overrideSetValues)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (instance == null) throw new ArgumentNullException(nameof(instance));

            var instanceType = instance.GetType();

            foreach (var property in instanceType
                .GetTypeInfo().DeclaredProperties
                .Where(pi => pi.CanWrite))
            {
                var propertyType = property.PropertyType;

                if (propertyType.GetTypeInfo().IsValueType && !propertyType.GetTypeInfo().IsEnum)
                    continue;

                if (propertyType.IsArray && propertyType.GetElementType().GetTypeInfo().IsValueType)
                    continue;

                if (propertyType.IsGenericEnumerableInterfaceType() && propertyType.GetTypeInfo().GenericTypeArguments.ToArray()[0].GetTypeInfo().IsValueType)
                    continue;

                if (property.GetIndexParameters().Length != 0)
                    continue;

                if (!context.IsRegistered(propertyType))
                    continue;

                if (!property.SetMethod.IsPublic)
                    continue;

                if (!overrideSetValues &&
                    property.CanRead && property.CanWrite &&
                    (property.GetValue(instance, null) != null))
                    continue;

                var propertyValue = context.Resolve(propertyType, new NamedParameter(InstanceTypeNamedParameter, instanceType));
                property.SetValue(instance, propertyValue, null);
            }
        }
开发者ID:arronchen,项目名称:Autofac,代码行数:40,代码来源:AutowiringPropertyInjector.cs

示例6: InjectProperties

        public static void InjectProperties(IComponentContext context, object instance, IPropertySelector propertySelector)
        {
            if (context == null) throw new ArgumentNullException(nameof(context));
            if (instance == null) throw new ArgumentNullException(nameof(instance));
            if (propertySelector == null) throw new ArgumentNullException(nameof(propertySelector));

            var instanceType = instance.GetType();

            foreach (var property in instanceType
                .GetRuntimeProperties()
                .Where(pi => pi.CanWrite))
            {
                var propertyType = property.PropertyType;

                if (propertyType.GetTypeInfo().IsValueType && !propertyType.GetTypeInfo().IsEnum)
                    continue;

                if (propertyType.IsArray && propertyType.GetElementType().GetTypeInfo().IsValueType)
                    continue;

                if (propertyType.IsGenericEnumerableInterfaceType() && propertyType.GetTypeInfo().GenericTypeArguments[0].GetTypeInfo().IsValueType)
                    continue;

                if (property.GetIndexParameters().Length != 0)
                    continue;

                if (!context.IsRegistered(propertyType))
                    continue;

                if (!propertySelector.InjectProperty(property, instance))
                    continue;

                var propertyValue = context.Resolve(propertyType, new NamedParameter(InstanceTypeNamedParameter, instanceType));
                property.SetValue(instance, propertyValue, null);
            }
        }
开发者ID:thegeekinside,项目名称:Autofac,代码行数:36,代码来源:AutowiringPropertyInjector.cs

示例7: GetServices

 private static IEnumerable<object> GetServices(IComponentContext context, Type serviceType)
 {
     return !context.IsRegistered(serviceType)
        ? Enumerable.Empty<object>()
        : GetServiceList(context, serviceType);
 }
开发者ID:asipe,项目名称:Nucs,代码行数:6,代码来源:DependencyResolver.cs

示例8: Init

        public static void Init(DatabaseInitMethod structureBootstrap, 
            IDbContext factory, IComponentContext ctx)
        {
            IndexRepository indexRepo = null;
            if (ctx.IsRegistered<IndexRepository>())
            {
                //cant take from context because IDbContext is initializing
                indexRepo = new IndexRepository(factory, log);
            }

            log.Info(String.Format("Initializing DB {0} {1} index module",
                structureBootstrap, (indexRepo==null?"without":"with")));

            switch (structureBootstrap)
            {
                case DatabaseInitMethod.DoNothing:
                    if(indexRepo!=null)
                    {
                        indexRepo.IndexManagers.ForEach(x => x.Optimize());
                    }
                    break;
                case DatabaseInitMethod.Recreate:
                    var schema = factory.GetSchemaExport();
                    schema.Create(true, true);

                    if (indexRepo != null)
                    {
                        indexRepo.IndexManagers.ForEach(x => x.Fill());
                    }
                   
                    break;
                case DatabaseInitMethod.FailIfInvalid:
                    try
                    {
                        factory.ValidateSchema();
                    }
                    catch (Exception ex)
                    {
                        throw new Exception("FailIfInvalid valiadtion failed", ex);
                    }
                    Init(DatabaseInitMethod.DoNothing, factory, ctx);
                    break;
                case DatabaseInitMethod.CreateIfInvalid:
                    var newstructureBootstrapMethod = DatabaseInitMethod.DoNothing;
                    //Test connection
                    try
                    {
                        factory.ValidateSchema();
                        if (indexRepo != null)
                        {
                            indexRepo.IndexManagers.ForEach(x => x.Purge());
                            indexRepo.IndexManagers.ForEach(x => x.Fill());
                        }
                    }
                    catch (Exception ex)
                    {
                        log.Debug("CreateIfInvalid valiadtion failed");
                        log.Debug(ex);
                        newstructureBootstrapMethod = DatabaseInitMethod.Recreate;
                    }
                    Init(newstructureBootstrapMethod, factory, ctx);

                    break;
            }
        }
开发者ID:ikutsin,项目名称:BinaryAnalysis.Core,代码行数:65,代码来源:DbInitializer.cs


注:本文中的IComponentContext.IsRegistered方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。