本文整理汇总了C#中IComponentContext.ResolveLookup方法的典型用法代码示例。如果您正苦于以下问题:C# IComponentContext.ResolveLookup方法的具体用法?C# IComponentContext.ResolveLookup怎么用?C# IComponentContext.ResolveLookup使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类IComponentContext
的用法示例。
在下文中一共展示了IComponentContext.ResolveLookup方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的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 | 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;
}
}
}
示例2: CanSupplyValue
/// <summary>
/// Returns true if the parameter is able to provide a value to a particular site.
/// </summary>
/// <param name="pi">Constructor, method, or property-mutator parameter.</param>
/// <param name="context">The component context in which the value is being provided.</param>
/// <param name="valueProvider">If the result is true, the valueProvider parameter will
/// be set to a function that will lazily retrieve the parameter value. If the result is false,
/// will be set to null.</param>
/// <returns>True if a value can be supplied; otherwise, false.</returns>
public override bool CanSupplyValue(ParameterInfo pi, IComponentContext context, out Func<object> valueProvider)
{
IComponentRegistration registration;
var ts = new TypedService(pi.ParameterType);
if (context.ComponentRegistry.TryGetRegistration(ts, out registration))
{
var lookup = context.ResolveLookup(ts, registration, Enumerable.Empty<Parameter>());
try
{
valueProvider = lookup.Factory;
}
catch (DependencyResolutionException dre)
{
dre.Lookups.Push(lookup);
throw;
}
return true;
}
valueProvider = null;
return false;
}