本文整理汇总了C#中Microsoft.CodeAnalysis.SemanticModel.GetTypeInfo方法的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel.GetTypeInfo方法的具体用法?C# SemanticModel.GetTypeInfo怎么用?C# SemanticModel.GetTypeInfo使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类Microsoft.CodeAnalysis.SemanticModel
的用法示例。
在下文中一共展示了SemanticModel.GetTypeInfo方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: TryInitializeState
protected override bool TryInitializeState(
Document document, SemanticModel model, SyntaxNode node, CancellationToken cancellationToken,
out INamedTypeSymbol classType, out INamedTypeSymbol abstractClassType)
{
var baseClassNode = node as TypeSyntax;
if (baseClassNode != null && baseClassNode.Parent is BaseTypeSyntax &&
baseClassNode.Parent.IsParentKind(SyntaxKind.BaseList) &&
((BaseTypeSyntax)baseClassNode.Parent).Type == baseClassNode)
{
if (baseClassNode.Parent.Parent.IsParentKind(SyntaxKind.ClassDeclaration))
{
abstractClassType = model.GetTypeInfo(baseClassNode, cancellationToken).Type as INamedTypeSymbol;
cancellationToken.ThrowIfCancellationRequested();
if (abstractClassType.IsAbstractClass())
{
var classDecl = baseClassNode.Parent.Parent.Parent as ClassDeclarationSyntax;
classType = model.GetDeclaredSymbol(classDecl, cancellationToken) as INamedTypeSymbol;
return classType != null && abstractClassType != null;
}
}
}
classType = null;
abstractClassType = null;
return false;
}
示例2: AnalyzeBinaryExpression
private static void AnalyzeBinaryExpression(BinaryExpressionSyntax node, SemanticModel model, Action<Diagnostic> addDiagnostic)
{
var leftType = model.GetTypeInfo(node.Left).Type;
var rightType = model.GetTypeInfo(node.Right).Type;
if (leftType != null && rightType != null && leftType.SpecialType == SpecialType.System_String && rightType.SpecialType == SpecialType.System_String)
{
addDiagnostic(node.OperatorToken.GetLocation().CreateDiagnostic(Rule));
}
}
示例3: GetMissingEnumMembers
//--- Class Methods ---
public static IEnumerable<ISymbol> GetMissingEnumMembers(SwitchStatementSyntax switchNode, SemanticModel semanticModel, out IdentifierNameSyntax switchVariable)
{
// TODO (2016-02-25, steveb): what if the swich calls a function instead?
switchVariable = switchNode.Expression as IdentifierNameSyntax;
if(switchVariable == null) {
return Enumerable.Empty<ISymbol>();
}
var switchVariableTypeInfo = semanticModel.GetTypeInfo(switchVariable);
// check if we are switching over an enum
if((switchVariableTypeInfo.Type != null) && (switchVariableTypeInfo.Type.TypeKind == TypeKind.Enum)) {
// get all the enum values
var enumMembers = switchVariableTypeInfo.Type.GetMembers().Where(x => x.Kind == SymbolKind.Field).ToImmutableArray();
// get all case statements
var caseSwitchLabels = switchNode.Sections
.SelectMany(section => section.Labels)
.Select(label => label.DescendantNodes().OfType<MemberAccessExpressionSyntax>().FirstOrDefault())
.Where(memberAccess => memberAccess != null)
.Select(memberAccess => semanticModel.GetSymbolInfo(memberAccess).Symbol)
.ToImmutableHashSet();
// make sure we have all cases covered
return enumMembers.Where(x => !caseSwitchLabels.Contains(x)).ToImmutableArray();
}
return Enumerable.Empty<ISymbol>();
}
示例4: CanBeNull
static bool CanBeNull(SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken)
{
var info = semanticModel.GetTypeInfo(expression, cancellationToken);
if (info.ConvertedType.IsReferenceType || info.ConvertedType.IsNullableType())
return true;
return false;
}
开发者ID:yeaicc,项目名称:RefactoringEssentials,代码行数:7,代码来源:ConvertConditionalTernaryToNullCoalescingAnalyzer.cs
示例5: IsDeclarationConstFriendly
static bool IsDeclarationConstFriendly(LocalDeclarationStatementSyntax declaration, SemanticModel semanticModel)
{
// all variables could be const?
foreach (var variable in declaration.Declaration.Variables)
{
if (variable.Initializer == null) return false;
if (variable.Initializer.Value is InterpolatedStringExpressionSyntax) return false;
// is constant
var constantValue = semanticModel.GetConstantValue(variable.Initializer.Value);
var valueIsConstant = constantValue.HasValue;
if (!valueIsConstant) return false;
// if reference type, value is null?
var variableTypeName = declaration.Declaration.Type;
var variableType = semanticModel.GetTypeInfo(variableTypeName).ConvertedType;
if (variableType.IsReferenceType && variableType.SpecialType != SpecialType.System_String && constantValue.Value != null) return false;
// nullable?
if (variableType.OriginalDefinition?.SpecialType == SpecialType.System_Nullable_T) return false;
// value can be converted to variable type?
var conversion = semanticModel.ClassifyConversion(variable.Initializer.Value, variableType);
if (!conversion.Exists || conversion.IsUserDefined) return false;
}
return true;
}
示例6: IsTypeInferred
/// <summary>
/// Determines whether the specified TypeSyntax is actually 'var'.
/// </summary>
public static bool IsTypeInferred(this TypeSyntax typeSyntax, SemanticModel semanticModel)
{
if (!typeSyntax.IsVar)
{
return false;
}
if (semanticModel.GetAliasInfo(typeSyntax) != null)
{
return false;
}
var type = semanticModel.GetTypeInfo(typeSyntax).Type;
if (type == null)
{
return false;
}
if (type.Name == "var")
{
return false;
}
return true;
}
示例7: CheckExceptionType
internal static bool CheckExceptionType(SemanticModel model, ObjectCreationExpressionSyntax objectCreateExpression, out ExpressionSyntax paramNode)
{
paramNode = null;
var type = model.GetTypeInfo(objectCreateExpression).Type;
if (type == null)
return false;
if (type.Name == typeof(ArgumentException).Name)
{
if (objectCreateExpression.ArgumentList.Arguments.Count >= 2)
{
paramNode = objectCreateExpression.ArgumentList.Arguments[1].Expression;
}
return paramNode != null;
}
if (type.Name == typeof(ArgumentNullException).Name ||
type.Name == typeof(ArgumentOutOfRangeException).Name ||
type.Name == "DuplicateWaitObjectException")
{
if (objectCreateExpression.ArgumentList.Arguments.Count >= 1)
{
paramNode = objectCreateExpression.ArgumentList.Arguments[0].Expression;
}
return paramNode != null;
}
return false;
}
示例8: IsReportable
private bool IsReportable(VariableDeclarationSyntax variableDeclaration, SemanticModel semanticModel)
{
bool result;
TypeSyntax variableTypeName = variableDeclaration.Type;
if (variableTypeName.IsVar)
{
// Implicitly-typed variables cannot have multiple declarators. Short circuit if it does.
bool hasMultipleVariables = variableDeclaration.Variables.Skip(1).Any();
if(hasMultipleVariables == false)
{
// Special case: Ensure that 'var' isn't actually an alias to another type. (e.g. using var = System.String).
IAliasSymbol aliasInfo = semanticModel.GetAliasInfo(variableTypeName);
if (aliasInfo == null)
{
// Retrieve the type inferred for var.
ITypeSymbol type = semanticModel.GetTypeInfo(variableTypeName).ConvertedType;
// Special case: Ensure that 'var' isn't actually a type named 'var'.
if (type.Name.Equals("var", StringComparison.Ordinal) == false)
{
// Special case: Ensure that the type is not an anonymous type.
if (type.IsAnonymousType == false)
{
// Special case: Ensure that it's not a 'new' expression.
if (variableDeclaration.Variables.First().Initializer.Value.IsKind(SyntaxKind.ObjectCreationExpression))
{
result = false;
}
else
{
result = true;
}
}
else
{
result = false;
}
}
else
{
result = false;
}
}
else
{
result = false;
}
}
else
{
result = false;
}
}
else
{
result = false;
}
return result;
}
示例9: GetDiagnosticsForNode
protected void GetDiagnosticsForNode(SyntaxNode node, SemanticModel model, Action<Diagnostic> addDiagnostic)
{
var type = model.GetTypeInfo(node).Type;
if (type != null && TypeHasWeakIdentity(type, model))
{
addDiagnostic(node.CreateDiagnostic(Rule, type.ToDisplayString()));
}
}
示例10: AnyArgumentIsValueType
private static bool AnyArgumentIsValueType(ArgumentListSyntax argumentList, SemanticModel semanticModel)
{
return argumentList.Arguments.Any(argument =>
{
var type = semanticModel.GetTypeInfo(argument.Expression).Type;
return type != null && type.IsValueType;
});
}
示例11: RequireReturnStatement
internal static bool RequireReturnStatement(SemanticModel model, SyntaxNode lambda)
{
var typeInfo = model.GetTypeInfo(lambda);
var type = typeInfo.ConvertedType ?? typeInfo.Type;
if (type == null || !type.IsDelegateType())
return false;
var returnType = type.GetDelegateInvokeMethod().GetReturnType();
return returnType != null && returnType.SpecialType != SpecialType.System_Void;
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:9,代码来源:ConvertLambdaBodyExpressionToStatementCodeRefactoringProvider.cs
示例12: IsCast
internal static bool IsCast(SemanticModel model, SyntaxNode n, ITypeSymbol type)
{
var expr = n as ExpressionSyntax;
if (expr == null)
return false;
expr = expr.SkipParens();
var castExpr = expr as CastExpressionSyntax;
if (castExpr != null)
{
return model.GetTypeInfo(castExpr.Type).Type == type;
}
var binExpr = expr as BinaryExpressionSyntax;
if (binExpr != null)
{
return binExpr.IsKind(SyntaxKind.AsExpression) && model.GetTypeInfo(binExpr.Right).Type == type;
}
return false;
}
示例13: UnpackNullableValueAccess
internal static ExpressionSyntax UnpackNullableValueAccess(SemanticModel semanticModel, ExpressionSyntax expression, CancellationToken cancellationToken)
{
var expr = expression.SkipParens();
if (!expr.IsKind(SyntaxKind.SimpleMemberAccessExpression))
return expression;
var info = semanticModel.GetTypeInfo(((MemberAccessExpressionSyntax)expr).Expression, cancellationToken);
if (!info.ConvertedType.IsNullableType())
return expression;
return ((MemberAccessExpressionSyntax)expr).Expression;
}
开发者ID:yeaicc,项目名称:RefactoringEssentials,代码行数:10,代码来源:ConvertConditionalTernaryToNullCoalescingAnalyzer.cs
示例14: ShouldReportOnCollectionNode
internal static bool ShouldReportOnCollectionNode(SemanticModel semanticModel, SyntaxNode argument) {
const bool returnIfUnknown = false;
// if the variable is an invocation syntax we can assume that it was returned materialized, if it's not an extension method
switch(argument.Kind()) {
case SyntaxKind.InvocationExpression:
var methodCallInfo = semanticModel.GetSymbolInfo(argument);
if((methodCallInfo.Symbol != null) && (methodCallInfo.Symbol.Kind == SymbolKind.Method)) {
var mSymbol = (IMethodSymbol)methodCallInfo.Symbol;
var typeInfo = semanticModel.GetTypeInfo(argument);
// If the method is not an extension method, we assume it returned a materialized collection
return IsAbstractCollectionType(typeInfo) && mSymbol.IsExtensionMethod && mSymbol.ContainingNamespace.ToDisplayString().Equals("System.Linq");
}
break;
case SyntaxKind.IdentifierName:
var identifierInfo = semanticModel.GetSymbolInfo(argument);
// if this identifier came straight from a parameter, assume it is materialized
if(identifierInfo.Symbol != null && identifierInfo.Symbol.Kind == SymbolKind.Parameter) {
//TODO(2016-02-24, yurig): if parameter is modified locally, we should warn
return false;
}
// if this is a local identifier, look at where it is defined
if(identifierInfo.Symbol != null && identifierInfo.Symbol.Kind == SymbolKind.Local) {
var declaration = identifierInfo.Symbol.DeclaringSyntaxReferences.FirstOrDefault();
// if the declaration is an equals expression, dive into it.
var equalsExpression = declaration?.GetSyntax()
.ChildNodes()
.FirstOrDefault(x => x.Kind() == SyntaxKind.EqualsValueClause);
var firstChild = equalsExpression?.ChildNodes().FirstOrDefault();
if(firstChild != null) {
return ShouldReportOnCollectionNode(semanticModel, firstChild);
}
// if the variable was assigned somewhere else, find it
var containingClass = declaration?.GetSyntax().FirstAncestorOrSelf<MethodDeclarationSyntax>();
var localAssignment = containingClass?.DescendantNodes().OfType<AssignmentExpressionSyntax>()
.Where(x => x.Left.IsKind(SyntaxKind.IdentifierName))
.LastOrDefault(x => (x.Left as IdentifierNameSyntax).Identifier.Text.Equals(((IdentifierNameSyntax)argument).Identifier.Text));
if(localAssignment != null) {
return ShouldReportOnCollectionNode(semanticModel, localAssignment.Right);
}
}
break;
case SyntaxKind.SimpleMemberAccessExpression:
// Assume that member accesses are returned materialized
return false;
}
return returnIfUnknown;
}
示例15: GetThrownExceptionTypeSymbol
public ITypeSymbol GetThrownExceptionTypeSymbol(SemanticModel semanticModel, ThrowStatementSyntax throwStatementSyntax)
{
SemanticModel = semanticModel;
ThrowStatementSyntax = throwStatementSyntax;
SyntaxNode syntaxNodeToGetTypeOf = ThrowStatementSyntax.Expression;
if (syntaxNodeToGetTypeOf == null)
syntaxNodeToGetTypeOf = GetCatchClaue();
if (syntaxNodeToGetTypeOf == null)
return null; //Not sure this is possible....
return semanticModel.GetTypeInfo(syntaxNodeToGetTypeOf).Type;
}