本文整理汇总了C#中Microsoft.CodeAnalysis.SemanticModel类的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel类的具体用法?C# SemanticModel怎么用?C# SemanticModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SemanticModel类属于Microsoft.CodeAnalysis命名空间,在下文中一共展示了SemanticModel类的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: TryDetermineOverridableMembers
static bool TryDetermineOverridableMembers(SemanticModel semanticModel, SyntaxToken startToken, Accessibility seenAccessibility, out ISet<ISymbol> overridableMembers, CancellationToken cancellationToken)
{
var result = new HashSet<ISymbol>();
var containingType = semanticModel.GetEnclosingSymbol<INamedTypeSymbol>(startToken.SpanStart, cancellationToken);
if (containingType != null && !containingType.IsScriptClass && !containingType.IsImplicitClass)
{
if (containingType.TypeKind == TypeKind.Class || containingType.TypeKind == TypeKind.Struct)
{
var baseTypes = containingType.GetBaseTypes().Reverse();
foreach (var type in baseTypes)
{
cancellationToken.ThrowIfCancellationRequested();
// Prefer overrides in derived classes
RemoveOverriddenMembers(result, type, cancellationToken);
// Retain overridable methods
AddProtocolMembers(semanticModel, result, containingType, type, cancellationToken);
}
// Don't suggest already overridden members
RemoveOverriddenMembers(result, containingType, cancellationToken);
}
}
// Filter based on accessibility
if (seenAccessibility != Accessibility.NotApplicable)
{
result.RemoveWhere(m => m.DeclaredAccessibility != seenAccessibility);
}
overridableMembers = result;
return overridableMembers.Count > 0;
}
示例3: 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
示例4: GetDeclaredSymbol
private static ISymbol GetDeclaredSymbol(SemanticModel model, SyntaxNode node, bool getSymbol, CancellationToken cancellationToken)
{
if (!getSymbol)
{
return null;
}
var declaredSymbol = model.GetDeclaredSymbol(node, cancellationToken);
// For namespace declarations, GetDeclaredSymbol returns a compilation scoped namespace symbol,
// which includes declarations across the compilation, including those in referenced assemblies.
// However, we are only interested in the namespace symbol scoped to the compilation's source assembly.
var namespaceSymbol = declaredSymbol as INamespaceSymbol;
if (namespaceSymbol != null && namespaceSymbol.ConstituentNamespaces.Length > 1)
{
var assemblyToScope = model.Compilation.Assembly;
var assemblyScopedNamespaceSymbol = namespaceSymbol.ConstituentNamespaces.FirstOrDefault(ns => ns.ContainingAssembly == assemblyToScope);
if (assemblyScopedNamespaceSymbol != null)
{
Debug.Assert(assemblyScopedNamespaceSymbol.ConstituentNamespaces.Length == 1);
declaredSymbol = assemblyScopedNamespaceSymbol;
}
}
return declaredSymbol;
}
示例5: MemberMetricsCalculator
public MemberMetricsCalculator(SemanticModel semanticModel, Solution solution, IAsyncFactory<ISymbol, IMemberDocumentation> documentationFactory)
: base(semanticModel)
{
_solution = solution;
_documentationFactory = documentationFactory;
_nameResolver = new MemberNameResolver(Model);
}
示例6: TryGetThreadStaticAttribute
private static bool TryGetThreadStaticAttribute(SyntaxList<AttributeListSyntax> attributeLists, SemanticModel semanticModel, out AttributeSyntax threadStaticAttribute)
{
threadStaticAttribute = null;
if (!attributeLists.Any())
{
return false;
}
foreach (var attributeList in attributeLists)
{
foreach (var attribute in attributeList.Attributes)
{
var attributeType = semanticModel.GetTypeInfo(attribute).Type;
if (attributeType != null &&
attributeType.ToDisplayString() == ThreadStaticAttributeName)
{
threadStaticAttribute = attribute;
return true;
}
}
}
return false;
}
示例7: CreateOrdinalMemberAccess
internal SyntaxNode CreateOrdinalMemberAccess(SyntaxGenerator syntaxFactoryService, SemanticModel model)
{
var stringComparisonType = WellKnownTypes.StringComparison(model.Compilation);
return syntaxFactoryService.MemberAccessExpression(
syntaxFactoryService.TypeExpression(stringComparisonType),
syntaxFactoryService.IdentifierName(UseOrdinalStringComparisonAnalyzer.OrdinalText));
}
示例8: IsAcceptableOverload
protected static bool IsAcceptableOverload(IMethodSymbol methodSymbol, SemanticModel model)
{
var stringComparisonType = WellKnownTypes.StringComparison(model.Compilation);
return methodSymbol.IsStatic
? IsAcceptableStaticOverload(methodSymbol, stringComparisonType)
: IsAcceptableInstanceOverload(methodSymbol, stringComparisonType);
}
示例9: CreateUsing
public static SyntaxNode CreateUsing(SyntaxNode root, ObjectCreationExpressionSyntax objectCreation, SemanticModel semanticModel)
{
SyntaxNode newRoot;
if (objectCreation.Parent.IsKind(SyntaxKind.SimpleAssignmentExpression))
{
var assignmentExpression = (AssignmentExpressionSyntax)objectCreation.Parent;
var statement = assignmentExpression.Parent as ExpressionStatementSyntax;
var identitySymbol = (ILocalSymbol)semanticModel.GetSymbolInfo(assignmentExpression.Left).Symbol;
newRoot = UsedOutsideParentBlock(semanticModel, statement, identitySymbol)
? CreateRootAddingDisposeToEndOfMethod(root, statement, identitySymbol)
: CreateRootWithUsing(root, statement, u => u.WithExpression(assignmentExpression));
}
else if (objectCreation.Parent.IsKind(SyntaxKind.EqualsValueClause) && objectCreation.Parent.Parent.IsKind(SyntaxKind.VariableDeclarator))
{
var variableDeclarator = (VariableDeclaratorSyntax)objectCreation.Parent.Parent;
var variableDeclaration = (VariableDeclarationSyntax)variableDeclarator.Parent;
var statement = (LocalDeclarationStatementSyntax)variableDeclaration.Parent;
newRoot = CreateRootWithUsing(root, statement, u => u.WithDeclaration(variableDeclaration));
}
else
{
newRoot = CreateRootWithUsing(root, (ExpressionStatementSyntax)objectCreation.Parent, u => u.WithExpression(objectCreation));
}
return newRoot;
}
示例10: DiscoveryWalker
public DiscoveryWalker(int start, int end, SemanticModel model)
{
_model = model;
_start = start;
_end = end;
DefinedOutside = new Dictionary<string, string>();
}
示例11: TryGetTypes
protected bool TryGetTypes(
SyntaxNode expression,
SemanticModel semanticModel,
out INamedTypeSymbol source,
out INamedTypeSymbol destination)
{
source = null;
destination = null;
var info = semanticModel.GetSymbolInfo(expression);
var methodSymbol = info.Symbol as IMethodSymbol;
if (methodSymbol == null)
{
return false;
}
var compilation = semanticModel.Compilation;
var taskType = compilation.GetTypeByMetadataName("System.Threading.Tasks.Task");
if (taskType == null)
{
return false;
}
var returnType = methodSymbol.ReturnType as INamedTypeSymbol;
if (returnType == null)
{
return false;
}
source = taskType;
destination = returnType;
return true;
}
示例12: IsPure
public static bool IsPure(this InvocationExpressionSyntax methodInvocation, SemanticModel semanticModel)
{
Contract.Requires(methodInvocation != null);
Contract.Requires(semanticModel != null);
return new PureMethodVerifier(semanticModel).IsPure(methodInvocation);
}
示例13: IsImmutable
public static bool IsImmutable(this ITypeSymbol symbol, SemanticModel semanticModel)
{
Contract.Requires(symbol != null);
Contract.Requires(semanticModel != null);
return new PureMethodVerifier(semanticModel).IsImmutable(symbol);
}
示例14: ComputeReplacement
private static SyntaxNode ComputeReplacement(SemanticModel semanticModel, SyntaxNode node, CancellationToken cancellationToken)
{
var memberAccess = node.Parent as MemberAccessExpressionSyntax;
if (memberAccess != null)
{
if (node == memberAccess.Name)
{
node = memberAccess;
}
}
var type = semanticModel.GetSymbolInfo(node, cancellationToken).Symbol as INamedTypeSymbol;
PredefinedTypeSyntax typeSyntax;
if (!PredefinedSpecialTypes.TryGetValue(type.SpecialType, out typeSyntax))
{
return node;
}
SyntaxNode newNode;
if (node is CrefSyntax)
{
newNode = SyntaxFactory.TypeCref(typeSyntax);
}
else
{
newNode = typeSyntax;
}
return newNode.WithTriviaFrom(node).WithoutFormatting();
}
示例15: EvaluateImpl
protected override Task<EvaluationResult> EvaluateImpl(SyntaxNode node, SemanticModel semanticModel, Solution solution)
{
//// TODO: For this to be correct, we need flow analysis to determine if a given method
//// is actually invoked inside the current constructor. A method may be assigned to a
//// delegate which can be called inside or outside the constructor. A method may also
//// be called from within a lambda which is called inside or outside the constructor.
//// Currently, FxCop does not produce a warning if a virtual method is called indirectly
//// through a delegate or through a lambda.
var constructor = (ConstructorDeclarationSyntax)node;
var constructorSymbol = semanticModel.GetDeclaredSymbol(constructor);
var containingType = constructorSymbol.ContainingType;
if (
constructor.Body.DescendantNodes()
.Where(x => x.Kind() == SyntaxKind.InvocationExpression)
.Any(x => CallVirtualMethod((InvocationExpressionSyntax)x, semanticModel, containingType)))
{
var result = new EvaluationResult
{
Snippet = node.ToFullString(),
LinesOfCodeAffected = GetLinesOfCode(node)
};
return Task.FromResult(result);
}
return Task.FromResult<EvaluationResult>(null);
}