本文整理汇总了C#中ExpressionSyntax.FirstAncestorOrSelf方法的典型用法代码示例。如果您正苦于以下问题:C# ExpressionSyntax.FirstAncestorOrSelf方法的具体用法?C# ExpressionSyntax.FirstAncestorOrSelf怎么用?C# ExpressionSyntax.FirstAncestorOrSelf使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ExpressionSyntax
的用法示例。
在下文中一共展示了ExpressionSyntax.FirstAncestorOrSelf方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: CreateConditional
protected ExpressionSyntax CreateConditional(ExpressionSyntax whenTrue, ExpressionSyntax whenFalse, ITypeSymbol targetType)
{
Debug.Assert(whenTrue != null);
Debug.Assert(whenTrue.FirstAncestorOrSelf<CompilationUnitSyntax>() == SemanticModel.SyntaxTree.GetRoot());
Debug.Assert(whenFalse != null);
Debug.Assert(whenFalse.FirstAncestorOrSelf<CompilationUnitSyntax>() == SemanticModel.SyntaxTree.GetRoot());
Debug.Assert(targetType != null);
// If there is no conversion between when-true and when-false, we need to insert a cast in
// one or both of the branches.
if (!ConversionExists(whenTrue, whenFalse))
{
var whenTrueConversion = SemanticModel.ClassifyConversion(whenTrue, targetType);
var whenFalseConversion = SemanticModel.ClassifyConversion(whenFalse, targetType);
if (whenTrueConversion.IsExplicit)
{
whenTrue = whenTrue.CastTo(targetType);
}
else if (whenFalseConversion.IsExplicit)
{
whenFalse = whenFalse.CastTo(targetType);
}
else if (whenTrueConversion.IsImplicit && whenFalseConversion.IsImplicit)
{
whenTrue = whenTrue.CastTo(targetType);
}
}
var condition = IfStatement.Condition.Kind() == SyntaxKind.SimpleAssignmentExpression
? IfStatement.Condition.Parenthesize()
: IfStatement.Condition;
ExpressionSyntax result = SyntaxFactory.ConditionalExpression(condition, whenTrue, whenFalse);
// Ensure that the conditional is implicitly convertible to the target type; otherwise,
// insert a cast. We do this be speculatively determining the conversion classification
// of the conditional expression to the target type in the same scope as the original
// if-statement.
var conversion = SemanticModel.ClassifyConversion(IfStatement.Span.Start, result, targetType);
if (conversion.IsExplicit)
{
result = result.CastTo(targetType);
}
return result;
}
示例2: InsideNameOfExpression
private static bool InsideNameOfExpression(ExpressionSyntax expr, SemanticModel semanticModel)
{
var nameOfInvocationExpr = expr.FirstAncestorOrSelf<InvocationExpressionSyntax>(
invocationExpr =>
{
var expression = invocationExpr.Expression as IdentifierNameSyntax;
return (expression != null) && (expression.Identifier.Text == "nameof") &&
semanticModel.GetConstantValue(invocationExpr).HasValue &&
(semanticModel.GetTypeInfo(invocationExpr).Type.SpecialType == SpecialType.System_String);
});
return nameOfInvocationExpr != null;
}
示例3: InsideCrefReference
private static bool InsideCrefReference(ExpressionSyntax expr)
{
var crefAttribute = expr.FirstAncestorOrSelf<XmlCrefAttributeSyntax>();
return crefAttribute != null;
}
示例4: WillConflictWithExistingLocal
private static bool WillConflictWithExistingLocal(ExpressionSyntax expression, ExpressionSyntax simplifiedNode)
{
if (simplifiedNode.Kind() == SyntaxKind.IdentifierName && !SyntaxFacts.IsInNamespaceOrTypeContext(expression))
{
var identifierName = (IdentifierNameSyntax)simplifiedNode;
var enclosingDeclarationSpace = FindImmediatelyEnclosingLocalVariableDeclarationSpace(expression);
var enclosingMemberDeclaration = expression.FirstAncestorOrSelf<MemberDeclarationSyntax>();
if (enclosingDeclarationSpace != null && enclosingMemberDeclaration != null)
{
var locals = enclosingMemberDeclaration.GetLocalDeclarationMap()[identifierName.Identifier.ValueText];
foreach (var token in locals)
{
if (token.GetAncestors<SyntaxNode>().Contains(enclosingDeclarationSpace))
{
return true;
}
}
}
}
return false;
}
示例5: IsRethrowOfCaughtException
private static bool IsRethrowOfCaughtException(ExpressionSyntax thrown)
{
var containingCatch = thrown.FirstAncestorOrSelf<CatchClauseSyntax>();
if (containingCatch != null)
{
var catchDecl = containingCatch.Declaration;
if (catchDecl != null)
{
var caughtExId = catchDecl.Identifier;
if (thrown.IsKind(SyntaxKind.IdentifierName) && ((IdentifierNameSyntax)thrown).Identifier.IsEquivalentTo(caughtExId))
{
return true;
}
}
}
return false;
}
示例6: IsObtainedFromFactory
private bool IsObtainedFromFactory(ExpressionSyntax thrown)
{
if (thrown.IsKind(SyntaxKind.IdentifierName))
{
// throw SomeException; - exception property or field in same class
IdentifierNameSyntax name = (IdentifierNameSyntax)thrown;
var referencedSymbol = _semanticModel.GetSymbolInfo(name).Symbol;
var referencedSyntax = referencedSymbol.DeclaringSyntaxReferences.Single().GetSyntax();
var variableDecl = referencedSyntax as VariableDeclaratorSyntax;
if (variableDecl != null && variableDecl.Initializer != null)
{
var val = variableDecl.Initializer.Value;
return IsObtainedFromFactory(val);
}
else
{
var propertyDecl = referencedSyntax as PropertyDeclarationSyntax;
if (propertyDecl != null)
{
// since it is a bald declaration, it must be in the same type and hence the same assembly,
// and will therefore get picked up in the ObjectCreationExpressionSyntax handler
return true;
}
}
}
if (thrown.IsKind(SyntaxKind.InvocationExpression))
{
// throw Util.SomeException(task); - exception factory method
InvocationExpressionSyntax expr = (InvocationExpressionSyntax)thrown;
if (expr.Expression.IsKind(SyntaxKind.SimpleMemberAccessExpression))
{
return IsObtainedFromFactory(expr.Expression);
}
}
if (thrown.IsKind(SyntaxKind.SimpleMemberAccessExpression))
{
// throw Util.SomeException - exception property or field in other class
MemberAccessExpressionSyntax expr = (MemberAccessExpressionSyntax)thrown;
if (expr.Expression.IsKind(SyntaxKind.IdentifierName))
{
var typeSyntaxWhereThrowIsHappening = thrown.FirstAncestorOrSelf<TypeDeclarationSyntax>();
var typeWhereThrowIsHappening = _semanticModel.GetDeclaredSymbol(typeSyntaxWhereThrowIsHappening);
var type = _semanticModel.GetTypeInfo(expr.Expression);
if (type.Type.ContainingAssembly == typeWhereThrowIsHappening.ContainingAssembly)
{
return true;
}
}
}
return false;
}