本文整理汇总了C#中ParseContext.Configure方法的典型用法代码示例。如果您正苦于以下问题:C# ParseContext.Configure方法的具体用法?C# ParseContext.Configure怎么用?C# ParseContext.Configure使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ParseContext
的用法示例。
在下文中一共展示了ParseContext.Configure方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: BindDependency
private static void BindDependency(Type type, string dependencyName, string dependencyText, ParseContext parseContext)
{
var serviceConstructor = type.GetConstructor();
if (!serviceConstructor.isOk)
{
var message = string.Format("type [{0}] has ", type.FormatName());
throw new SimpleContainerException(message + serviceConstructor.errorMessage);
}
var formalParameter = serviceConstructor.value.GetParameters().SingleOrDefault(x => x.Name == dependencyName);
if (formalParameter == null)
{
const string message = "type [{0}] has no dependency [{1}]";
throw new SimpleContainerException(string.Format(message, type.FormatName(), dependencyName));
}
var targetType = formalParameter.ParameterType;
var underlyingType = Nullable.GetUnderlyingType(targetType);
if (underlyingType != null)
targetType = underlyingType;
IConvertible convertible = dependencyText;
object parsedValue;
try
{
parsedValue = convertible.ToType(targetType, CultureInfo.InvariantCulture);
}
catch (Exception e)
{
const string message = "can't parse [{0}.{1}] from [{2}] as [{3}]";
throw new SimpleContainerException(string.Format(message, type.FormatName(), dependencyName,
dependencyText, formalParameter.ParameterType.FormatName()), e);
}
parseContext.Configure(type, b => b.BindDependency(dependencyName, parsedValue));
}