本文整理汇总了C#中Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol.CheckConstraints方法的典型用法代码示例。如果您正苦于以下问题:C# MethodSymbol.CheckConstraints方法的具体用法?C# MethodSymbol.CheckConstraints怎么用?C# MethodSymbol.CheckConstraints使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.CSharp.Symbols.MethodSymbol
的用法示例。
在下文中一共展示了MethodSymbol.CheckConstraints方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: MemberGroupFinalValidation
/// <summary>
/// This method implements the algorithm in spec section 7.6.5.1.
///
/// For method group conversions, there are situations in which the conversion is
/// considered to exist ("Otherwise the algorithm produces a single best method M having
/// the same number of parameters as D and the conversion is considered to exist"), but
/// application of the conversion fails. These are the "final validation" steps of
/// overload resolution.
/// </summary>
/// <returns>
/// True if there is any error.
/// </returns>
private bool MemberGroupFinalValidation(BoundExpression receiverOpt, MethodSymbol methodSymbol, CSharpSyntaxNode node, DiagnosticBag diagnostics, bool invokedAsExtensionMethod)
{
if (MemberGroupFinalValidationAccessibilityChecks(receiverOpt, methodSymbol, node, diagnostics, invokedAsExtensionMethod))
{
return true;
}
// SPEC: If the best method is a generic method, the type arguments (supplied or inferred) are checked against the constraints
// SPEC: declared on the generic method. If any type argument does not satisfy the corresponding constraint(s) on
// SPEC: the type parameter, a binding-time error occurs.
// The portion of the overload resolution spec quoted above is subtle and somewhat
// controversial. The upshot of this is that overload resolution does not consider
// constraints to be a part of the signature. Overload resolution matches arguments to
// parameter lists; it does not consider things which are outside of the parameter list.
// If the best match from the arguments to the formal parameters is not viable then we
// give an error rather than falling back to a worse match.
//
// Consider the following:
//
// void M<T>(T t) where T : Reptile {}
// void M(object x) {}
// ...
// M(new Giraffe());
//
// The correct analysis is to determine that the applicable candidates are
// M<Giraffe>(Giraffe) and M(object). Overload resolution then chooses the former
// because it is an exact match, over the latter which is an inexact match. Only after
// the best method is determined do we check the constraints and discover that the
// constraint on T has been violated.
//
// Note that this is different from the rule that says that during type inference, if an
// inference violates a constraint then inference fails. For example:
//
// class C<T> where T : struct {}
// ...
// void M<U>(U u, C<U> c){}
// void M(object x, object y) {}
// ...
// M("hello", null);
//
// Type inference determines that U is string, but since C<string> is not a valid type
// because of the constraint, type inference fails. M<string> is never added to the
// applicable candidate set, so the applicable candidate set consists solely of
// M(object, object) and is therefore the best match.
return !methodSymbol.CheckConstraints(this.Conversions, node, this.Compilation, diagnostics);
}