本文整理汇总了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);
}
}
}
示例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;
}
}
}
示例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);
}
示例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);
}
}
示例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);
}
}
示例7: GetServices
private static IEnumerable<object> GetServices(IComponentContext context, Type serviceType)
{
return !context.IsRegistered(serviceType)
? Enumerable.Empty<object>()
: GetServiceList(context, serviceType);
}
示例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;
}
}