本文整理汇总了C#中SemanticModel.ClassifyConversion方法的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel.ClassifyConversion方法的具体用法?C# SemanticModel.ClassifyConversion怎么用?C# SemanticModel.ClassifyConversion使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SemanticModel
的用法示例。
在下文中一共展示了SemanticModel.ClassifyConversion方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ConversionTestHelper
/// <summary>
///
/// </summary>
/// <param name="binding"></param>
/// <param name="expr"></param>
/// <param name="ept1">expr -> TypeInParent</param>
/// <param name="ept2">Type(expr) -> TypeInParent</param>
private void ConversionTestHelper(SemanticModel semanticModel, ExpressionSyntax expr, ConversionKind ept1, ConversionKind ept2)
{
var info = semanticModel.GetTypeInfo(expr);
Assert.NotNull(info);
Assert.NotNull(info.ConvertedType);
var conv = semanticModel.GetConversion(expr);
// NOT expect NoConversion
Conversion act1 = semanticModel.ClassifyConversion(expr, (TypeSymbol)info.ConvertedType);
Assert.Equal(ept1, act1.Kind);
ValidateConversion(act1, ept1);
ValidateConversion(act1, conv.Kind);
if (ept2 == ConversionKind.NoConversion)
{
Assert.Null(info.Type);
}
else
{
Assert.NotNull(info.Type);
var act2 = semanticModel.Compilation.ClassifyConversion(info.Type, info.ConvertedType);
Assert.Equal(ept2, act2.Kind);
ValidateConversion(act2, ept2);
}
}
示例2: IsUnnecessaryCast
public static bool IsUnnecessaryCast(this CastExpressionSyntax cast, SemanticModel semanticModel, CancellationToken cancellationToken)
{
var speculationAnalyzer = new SpeculationAnalyzer(cast,
cast.Expression, semanticModel, cancellationToken,
skipVerificationForReplacedNode: true, failOnOverloadResolutionFailuresInOriginalCode: true);
// First, check to see if the node ultimately parenting this cast has any
// syntax errors. If so, we bail.
if (speculationAnalyzer.SemanticRootOfOriginalExpression.ContainsDiagnostics)
{
return false;
}
var castTypeInfo = semanticModel.GetTypeInfo(cast, cancellationToken);
var castType = castTypeInfo.Type;
// Case:
// 1 . Console.WriteLine(await (dynamic)task); Any Dynamic Cast will not be removed.
if (castType == null || castType.Kind == SymbolKind.DynamicType || castType.IsErrorType())
{
return false;
}
var expressionTypeInfo = semanticModel.GetTypeInfo(cast.Expression, cancellationToken);
var expressionType = expressionTypeInfo.Type;
// We do not remove any cast on
// 1. Dynamic Expressions
// 2. If there is any other argument which is dynamic
// 3. Dynamic Invocation
if ((expressionType != null &&
(expressionType.IsErrorType() ||
expressionType.Kind == SymbolKind.DynamicType)) ||
IsDynamicInvocation(cast, semanticModel, cancellationToken))
{
return false;
}
if (PointerCastDefinitelyCantBeRemoved(cast))
{
return false;
}
if (CastPassedToParamsArrayDefinitelyCantBeRemoved(cast, castType, semanticModel, cancellationToken))
{
return false;
}
if (speculationAnalyzer.ReplacementChangesSemantics())
{
return false;
}
var expressionToCastType = semanticModel.ClassifyConversion(cast.SpanStart, cast.Expression, castType, isExplicitInSource: true);
bool parentIsOrAsExpression;
var outerType = GetOuterCastType(cast, semanticModel, out parentIsOrAsExpression) ?? castTypeInfo.ConvertedType;
// Simple case: If the conversion from the inner expression to the cast type is identity,
// the cast can be removed.
if (expressionToCastType.IsIdentity)
{
// Required explicit cast for reference comparison.
// Cast removal causes warning CS0252 (Possible unintended reference comparison).
// object x = string.Intern("Hi!");
// (object)x == "Hi!"
ExpressionSyntax other;
if (IsRequiredCastForReferenceEqualityComparison(outerType, cast, semanticModel, out other))
{
var otherToOuterType = semanticModel.ClassifyConversion(other, outerType);
if (otherToOuterType.IsImplicit && otherToOuterType.IsReference)
{
return false;
}
}
return true;
}
else if (expressionToCastType.IsExplicit && expressionToCastType.IsReference)
{
// Explicit reference conversions can cause an exception or data loss, hence can never be removed.
return false;
}
else if (expressionToCastType.IsExplicit && expressionToCastType.IsNumeric && IsInExplicitCheckedOrUncheckedContext(cast))
{
// Don't remove any explicit numeric casts in explicit checked/unchecked context.
// https://github.com/dotnet/roslyn/issues/2987 tracks improving on this conservative approach.
return false;
}
else if (expressionToCastType.IsPointer)
{
// Don't remove any non-identity pointer conversions.
// https://github.com/dotnet/roslyn/issues/2987 tracks improving on this conservative approach.
return expressionType != null && expressionType.Equals(outerType);
}
if (parentIsOrAsExpression)
{
// Note: speculationAnalyzer.ReplacementChangesSemantics() ensures that the parenting is or as expression are not broken.
// Here we just need to ensure that the original cast expression doesn't invoke a user defined operator.
//.........这里部分代码省略.........