本文整理汇总了C#中System.Reflection.ParameterInfo.TryGetCustomAttribute方法的典型用法代码示例。如果您正苦于以下问题:C# ParameterInfo.TryGetCustomAttribute方法的具体用法?C# ParameterInfo.TryGetCustomAttribute怎么用?C# ParameterInfo.TryGetCustomAttribute使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Reflection.ParameterInfo
的用法示例。
在下文中一共展示了ParameterInfo.TryGetCustomAttribute方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InstantiateDependency
internal ServiceDependency InstantiateDependency(ParameterInfo formalParameter, ContainerService.Builder builder)
{
ValueWithType actualArgument;
if (builder.Arguments != null && builder.Arguments.TryGet(formalParameter.Name, out actualArgument))
return containerContext.Constant(formalParameter, actualArgument.value);
var parameters = builder.Configuration.ParametersSource;
object actualParameter;
if (parameters != null && parameters.TryGet(formalParameter.Name, formalParameter.ParameterType, out actualParameter))
return containerContext.Constant(formalParameter, actualParameter);
var dependencyConfiguration = builder.Configuration.GetOrNull(formalParameter);
Type implementationType = null;
if (dependencyConfiguration != null)
{
if (dependencyConfiguration.ValueAssigned)
return containerContext.Constant(formalParameter, dependencyConfiguration.Value);
if (dependencyConfiguration.Factory != null)
{
var dependencyBuilder = new ContainerService.Builder(new ServiceName(formalParameter.ParameterType))
{
Context = builder.Context,
DependencyName = formalParameter.Name
};
builder.Context.Stack.Add(dependencyBuilder);
dependencyBuilder.CreateInstanceBy(CallTarget.F(dependencyConfiguration.Factory), true);
builder.Context.Stack.RemoveLast();
return dependencyBuilder.GetService().AsDependency(containerContext, formalParameter.Name, false);
}
implementationType = dependencyConfiguration.ImplementationType;
}
implementationType = implementationType ?? formalParameter.ParameterType;
FromResourceAttribute resourceAttribute;
if (implementationType == typeof (Stream) && formalParameter.TryGetCustomAttribute(out resourceAttribute))
{
var resourceStream = builder.Type.Assembly.GetManifestResourceStream(builder.Type, resourceAttribute.Name);
if (resourceStream == null)
return containerContext.Error(null, formalParameter.Name,
"can't find resource [{0}] in namespace of [{1}], assembly [{2}]",
resourceAttribute.Name, builder.Type, builder.Type.Assembly.GetName().Name);
return containerContext.Resource(formalParameter, resourceAttribute.Name, resourceStream);
}
var dependencyName = ServiceName.Parse(implementationType.UnwrapEnumerable(),
InternalHelpers.ParseContracts(formalParameter));
if (dependencyName.Type.IsSimpleType())
{
if (!formalParameter.HasDefaultValue)
return containerContext.Error(null, formalParameter.Name,
"parameter [{0}] of service [{1}] is not configured",
formalParameter.Name, builder.Type.FormatName());
return containerContext.Constant(formalParameter, formalParameter.DefaultValue);
}
var resultService = ResolveCore(dependencyName, false, null, builder.Context);
if (resultService.Status.IsBad())
return containerContext.ServiceError(resultService);
var isEnumerable = dependencyName.Type != implementationType;
if (isEnumerable)
return containerContext.Service(resultService, resultService.GetAllValues());
if (resultService.Status == ServiceStatus.NotResolved)
{
if (formalParameter.HasDefaultValue)
return containerContext.Service(resultService, formalParameter.DefaultValue);
if (formalParameter.IsDefined<OptionalAttribute>() || formalParameter.IsDefined("CanBeNullAttribute"))
return containerContext.Service(resultService, null);
return containerContext.NotResolved(resultService);
}
return resultService.AsDependency(containerContext, null, false);
}