本文整理汇总了C#中SemanticModel.GetConstantValue方法的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel.GetConstantValue方法的具体用法?C# SemanticModel.GetConstantValue怎么用?C# SemanticModel.GetConstantValue使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SemanticModel
的用法示例。
在下文中一共展示了SemanticModel.GetConstantValue方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: 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;
}
示例2: CanSimplifyToLengthEqualsZeroExpression
private bool CanSimplifyToLengthEqualsZeroExpression(
ExpressionSyntax variableExpression,
LiteralExpressionSyntax numericLiteralExpression,
SemanticModel semanticModel,
CancellationToken cancellationToken)
{
var numericValue = semanticModel.GetConstantValue(numericLiteralExpression, cancellationToken);
if (numericValue.HasValue && numericValue.Value is int && (int)numericValue.Value == 0)
{
var symbol = semanticModel.GetSymbolInfo(variableExpression, cancellationToken).Symbol;
if (symbol != null && (symbol.Name == "Length" || symbol.Name == "LongLength"))
{
var containingType = symbol.ContainingType;
if (containingType != null &&
(containingType.SpecialType == SpecialType.System_Array ||
containingType.SpecialType == SpecialType.System_String))
{
return true;
}
}
var typeInfo = semanticModel.GetTypeInfo(variableExpression, cancellationToken);
if (typeInfo.Type != null)
{
switch (typeInfo.Type.SpecialType)
{
case SpecialType.System_Byte:
case SpecialType.System_UInt16:
case SpecialType.System_UInt32:
case SpecialType.System_UInt64:
return true;
}
}
}
return false;
}