本文整理匯總了C#中System.Arguments.FilteredFor方法的典型用法代碼示例。如果您正苦於以下問題:C# Arguments.FilteredFor方法的具體用法?C# Arguments.FilteredFor怎麽用?C# Arguments.FilteredFor使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類System.Arguments
的用法示例。
在下文中一共展示了Arguments.FilteredFor方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的C#代碼示例。
示例1: GetBestMatch
ConstructorData GetBestMatch(IEnumerable<ConstructorInfo> constructors, Arguments args, Dictionary<ConstructorInfo, IList<ParameterInfo>> unresolved)
{
var ctors = constructors.Select(c => new ConstructorMatchData
{
Constructor = c,
Parameters = c.GetParameters().ToList(),
ParameterDelta = c.GetParameters().Count() - args.Count(), /* negative number here is most likely (and wanted!) */
MatchLength = 0,
Score = 0
});
foreach (var ctor in ctors)
{
for (var i = 0; i < Math.Min(ctor.Parameters.Count(), args.Count()); i++)
{
if (ctor.Parameters[i].Name != args[i].Name || ctor.Parameters[i].ParameterType != args[i].Value.GetType())
{
break;
}
ctor.MatchLength = ctor.MatchLength + 1;
}
}
foreach (var ctor in ctors)
{
ctor.Score = ctor.MatchLength + ctor.ParameterDelta;
}
//check if the top item has a unique score
var best = ctors.Where(c => c.Score == ctors.OrderBy(t => t.Score).First().Score);
if (best.Count() == 1)
{
return new ConstructorData(best.First().Constructor, args.FilteredFor(best.First().Constructor));
}
//apply the delta again to try and distinct the matches
foreach (var ctor in ctors)
{
ctor.Score += ctor.ParameterDelta;
}
//check if the top item has a unique score
best = ctors.Where(c => c.Score == ctors.OrderBy(t => t.Score).First().Score);
if (best.Count() == 1)
{
return new ConstructorData(best.First().Constructor, args.FilteredFor(best.First().Constructor));
}
throw new ConstructorException();
}