本文整理汇总了C#中BoundExpression.AsImmutable方法的典型用法代码示例。如果您正苦于以下问题:C# BoundExpression.AsImmutable方法的具体用法?C# BoundExpression.AsImmutable怎么用?C# BoundExpression.AsImmutable使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类BoundExpression
的用法示例。
在下文中一共展示了BoundExpression.AsImmutable方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: InferExtensionMethodTypeArguments
/// <summary>
/// If the extension method is applicable based on the "this" argument type, return
/// the method constructed with the inferred type arguments. If the method is not an
/// unconstructed generic method, type inference is skipped. If the method is not
/// applicable, or if constraints when inferring type parameters from the "this" type
/// are not satisfied, the return value is null.
/// </summary>
public static MethodSymbol InferExtensionMethodTypeArguments(this MethodSymbol method, TypeSymbol thisType, Compilation compilation, ref HashSet<DiagnosticInfo> useSiteDiagnostics)
{
Debug.Assert(method.IsExtensionMethod);
Debug.Assert((object)thisType != null);
if (!method.IsGenericMethod || method != method.ConstructedFrom)
{
return method;
}
// We never resolve extension methods on a dynamic receiver.
if (thisType.IsDynamic())
{
return null;
}
var containingAssembly = method.ContainingAssembly;
var errorNamespace = containingAssembly.GlobalNamespace;
var conversions = new TypeConversions(containingAssembly.CorLibrary);
// There is absolutely no plausible syntax/tree that we could use for these
// synthesized literals. We could be speculatively binding a call to a PE method.
var syntaxTree = CSharpSyntaxTree.Dummy;
var syntax = (CSharpSyntaxNode)syntaxTree.GetRoot();
// Create an argument value for the "this" argument of specific type,
// and pass the same bad argument value for all other arguments.
var thisArgumentValue = new BoundLiteral(syntax, ConstantValue.Bad, thisType) { WasCompilerGenerated = true };
var otherArgumentType = new ExtendedErrorTypeSymbol(errorNamespace, name: string.Empty, arity: 0, errorInfo: null, unreported: false);
var otherArgumentValue = new BoundLiteral(syntax, ConstantValue.Bad, otherArgumentType) { WasCompilerGenerated = true };
var paramCount = method.ParameterCount;
var arguments = new BoundExpression[paramCount];
for (int i = 0; i < paramCount; i++)
{
var argument = (i == 0) ? thisArgumentValue : otherArgumentValue;
arguments[i] = argument;
}
var typeArgs = MethodTypeInferrer.InferTypeArgumentsFromFirstArgument(
conversions,
method,
arguments.AsImmutable(),
ref useSiteDiagnostics);
if (typeArgs.IsDefault)
{
return null;
}
int firstNullInTypeArgs = -1;
// For the purpose of constraint checks we use error type symbol in place of type arguments that we couldn't infer from the first argument.
// This prevents constraint checking from failing for corresponding type parameters.
var notInferredTypeParameters = PooledHashSet<TypeParameterSymbol>.GetInstance();
var typeParams = method.TypeParameters;
var typeArgsForConstraintsCheck = typeArgs;
for (int i = 0; i < typeArgsForConstraintsCheck.Length; i++)
{
if ((object)typeArgsForConstraintsCheck[i] == null)
{
firstNullInTypeArgs = i;
var builder = ArrayBuilder<TypeSymbol>.GetInstance();
builder.AddRange(typeArgs, firstNullInTypeArgs);
for (; i < typeArgsForConstraintsCheck.Length; i++)
{
var typeArg = typeArgsForConstraintsCheck[i];
if ((object)typeArg == null)
{
notInferredTypeParameters.Add(typeParams[i]);
builder.Add(ErrorTypeSymbol.UnknownResultType);
}
else
{
builder.Add(typeArg);
}
}
typeArgsForConstraintsCheck = builder.ToImmutableAndFree();
break;
}
}
// Check constraints.
var diagnosticsBuilder = ArrayBuilder<TypeParameterDiagnosticInfo>.GetInstance();
var substitution = new TypeMap(typeParams, typeArgsForConstraintsCheck.SelectAsArray(TypeMap.TypeSymbolAsTypeWithModifiers));
ArrayBuilder<TypeParameterDiagnosticInfo> useSiteDiagnosticsBuilder = null;
var success = method.CheckConstraints(conversions, substitution, typeParams, typeArgsForConstraintsCheck, compilation, diagnosticsBuilder, ref useSiteDiagnosticsBuilder,
ignoreTypeConstraintsDependentOnTypeParametersOpt: notInferredTypeParameters.Count > 0 ? notInferredTypeParameters : null);
diagnosticsBuilder.Free();
notInferredTypeParameters.Free();
//.........这里部分代码省略.........