本文整理汇总了C#中SemanticModel.LookupSymbols方法的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel.LookupSymbols方法的具体用法?C# SemanticModel.LookupSymbols怎么用?C# SemanticModel.LookupSymbols使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SemanticModel
的用法示例。
在下文中一共展示了SemanticModel.LookupSymbols方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetMethodGroupItems
private IEnumerable<SignatureHelpItem> GetMethodGroupItems(
InvocationExpressionSyntax invocationExpression,
SemanticModel semanticModel,
ISymbolDisplayService symbolDisplayService,
IAnonymousTypeDisplayService anonymousTypeDisplayService,
IDocumentationCommentFormattingService documentationCommentFormattingService,
ISymbol within,
IEnumerable<IMethodSymbol> methodGroup,
CancellationToken cancellationToken)
{
ITypeSymbol throughType = null;
if (invocationExpression.Expression is MemberAccessExpressionSyntax)
{
var throughExpression = ((MemberAccessExpressionSyntax)invocationExpression.Expression).Expression;
var throughSymbol = semanticModel.GetSymbolInfo(throughExpression, cancellationToken).GetAnySymbol();
// if it is via a base expression "base.", we know the "throughType" is the base class but
// we need to be able to tell between "base.M()" and "new Base().M()".
// currently, Access check methods do not differentiate between them.
// so handle "base." primary-expression here by nulling out "throughType"
if (!(throughExpression is BaseExpressionSyntax))
{
throughType = semanticModel.GetTypeInfo(throughExpression, cancellationToken).Type;
}
var includeInstance = !throughExpression.IsKind(SyntaxKind.IdentifierName) ||
semanticModel.LookupSymbols(throughExpression.SpanStart, name: throughSymbol.Name).Any(s => !(s is INamedTypeSymbol)) ||
(!(throughSymbol is INamespaceOrTypeSymbol) && semanticModel.LookupSymbols(throughExpression.SpanStart, container: throughSymbol.ContainingType).Any(s => !(s is INamedTypeSymbol)));
var includeStatic = throughSymbol is INamedTypeSymbol ||
(throughExpression.IsKind(SyntaxKind.IdentifierName) &&
semanticModel.LookupNamespacesAndTypes(throughExpression.SpanStart, name: throughSymbol.Name).Any(t => t.GetSymbolType() == throughType));
Contract.ThrowIfFalse(includeInstance || includeStatic);
methodGroup = methodGroup.Where(m => (m.IsStatic && includeStatic) || (!m.IsStatic && includeInstance));
}
else if (invocationExpression.Expression is SimpleNameSyntax &&
invocationExpression.IsInStaticContext())
{
methodGroup = methodGroup.Where(m => m.IsStatic);
}
var accessibleMethods = methodGroup.Where(m => m.IsAccessibleWithin(within, throughTypeOpt: throughType)).ToList();
if (accessibleMethods.Count == 0)
{
return null;
}
var methodSet = accessibleMethods.ToSet();
accessibleMethods = accessibleMethods.Where(m => !IsHiddenByOtherMethod(m, methodSet)).ToList();
return accessibleMethods.Select(m =>
ConvertMethodGroupMethod(m, invocationExpression, semanticModel, symbolDisplayService, anonymousTypeDisplayService, documentationCommentFormattingService, cancellationToken));
}
示例2: GetCompletionsOffOfExplicitInterfaceAsync
private async Task<IEnumerable<CompletionItem>> GetCompletionsOffOfExplicitInterfaceAsync(
Document document, SemanticModel semanticModel, int position, NameSyntax name, CancellationToken cancellationToken)
{
// Bind the interface name which is to the left of the dot
var syntaxTree = semanticModel.SyntaxTree;
var nameBinding = semanticModel.GetSymbolInfo(name, cancellationToken);
var context = CSharpSyntaxContext.CreateContext(document.Project.Solution.Workspace, semanticModel, position, cancellationToken);
var symbol = nameBinding.Symbol as ITypeSymbol;
if (symbol == null || symbol.TypeKind != TypeKind.Interface)
{
return SpecializedCollections.EmptyEnumerable<CompletionItem>();
}
var members = semanticModel.LookupSymbols(
position: name.SpanStart,
container: symbol)
.Where(s => !s.IsStatic)
.FilterToVisibleAndBrowsableSymbols(document.ShouldHideAdvancedMembers(), semanticModel.Compilation);
// We're going to create a entry for each one, including the signature
var completions = new List<CompletionItem>();
var signatureDisplayFormat =
new SymbolDisplayFormat(
genericsOptions: SymbolDisplayGenericsOptions.IncludeTypeParameters,
memberOptions:
SymbolDisplayMemberOptions.IncludeParameters,
parameterOptions:
SymbolDisplayParameterOptions.IncludeName |
SymbolDisplayParameterOptions.IncludeType |
SymbolDisplayParameterOptions.IncludeParamsRefOut,
miscellaneousOptions:
SymbolDisplayMiscellaneousOptions.EscapeKeywordIdentifiers |
SymbolDisplayMiscellaneousOptions.UseSpecialTypes);
var namePosition = name.SpanStart;
var text = await context.SyntaxTree.GetTextAsync(cancellationToken).ConfigureAwait(false);
var textChangeSpan = CompletionUtilities.GetTextChangeSpan(text, context.Position);
foreach (var member in members)
{
var displayString = member.ToMinimalDisplayString(semanticModel, namePosition, signatureDisplayFormat);
var memberCopied = member;
var insertionText = displayString;
completions.Add(new SymbolCompletionItem(
this,
displayString,
insertionText: insertionText,
filterSpan: textChangeSpan,
position: position,
symbols: new List<ISymbol> { member },
context: context,
rules: ItemRules.Instance));
}
return completions;
}
示例3: VerifyModelForDeclarationPattern
protected static void VerifyModelForDeclarationPattern(
SemanticModel model,
DeclarationPatternSyntax decl,
bool isShadowed,
params IdentifierNameSyntax[] references)
{
var symbol = model.GetDeclaredSymbol(decl);
Assert.Equal(decl.Identifier.ValueText, symbol.Name);
Assert.Equal(decl, symbol.DeclaringSyntaxReferences.Single().GetSyntax());
Assert.Equal(LocalDeclarationKind.PatternVariable, ((LocalSymbol)symbol).DeclarationKind);
Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)decl));
if (isShadowed)
{
Assert.NotEqual(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single());
}
else
{
Assert.Same(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single());
}
Assert.True(model.LookupNames(decl.SpanStart).Contains(decl.Identifier.ValueText));
Assert.True(SyntaxFacts.IsInNamespaceOrTypeContext(decl.Type));
Assert.True(SyntaxFacts.IsInTypeOnlyContext(decl.Type));
var local = ((SourceLocalSymbol)symbol);
var type = local.Type;
if (type.IsErrorType())
{
Assert.Null(model.GetSymbolInfo(decl.Type).Symbol);
}
else
{
Assert.Equal(type, model.GetSymbolInfo(decl.Type).Symbol);
}
foreach (var reference in references)
{
Assert.Same(symbol, model.GetSymbolInfo(reference).Symbol);
Assert.Same(symbol, model.LookupSymbols(reference.SpanStart, name: decl.Identifier.ValueText).Single());
Assert.True(model.LookupNames(reference.SpanStart).Contains(decl.Identifier.ValueText));
}
}
示例4: LookupTypeParameterFromConstraintClause
private static TypeParameterSymbol LookupTypeParameterFromConstraintClause(SemanticModel model, TypeParameterConstraintClauseSyntax constraintSyntax, string name)
{
var constraintStart = constraintSyntax.WhereKeyword.SpanStart;
var symbols = model.LookupSymbols(constraintStart, null, name: name);
Assert.Equal(1, symbols.Length);
var symbol = symbols[0] as TypeParameterSymbol;
Assert.NotNull(symbol);
return symbol;
}
示例5: VerifyNotAPatternLocal
private static void VerifyNotAPatternLocal(SemanticModel model, IdentifierNameSyntax reference)
{
var symbol = model.GetSymbolInfo(reference).Symbol;
if (symbol.Kind == SymbolKind.Local)
{
Assert.NotEqual(LocalDeclarationKind.PatternVariable, ((LocalSymbol)symbol).DeclarationKind);
}
Assert.Same(symbol, model.LookupSymbols(reference.SpanStart, name: reference.Identifier.ValueText).Single());
Assert.True(model.LookupNames(reference.SpanStart).Contains(reference.Identifier.ValueText));
}
示例6: VerifyModelForDeclarationPattern
private static void VerifyModelForDeclarationPattern(SemanticModel model, DeclarationPatternSyntax decl, params IdentifierNameSyntax[] references)
{
var symbol = model.GetDeclaredSymbol(decl);
Assert.Equal(decl.Identifier.ValueText, symbol.Name);
Assert.Equal(LocalDeclarationKind.PatternVariable, ((LocalSymbol)symbol).DeclarationKind);
Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)decl));
Assert.Same(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single());
Assert.True(model.LookupNames(decl.SpanStart).Contains(decl.Identifier.ValueText));
foreach (var reference in references)
{
Assert.Same(symbol, model.GetSymbolInfo(reference).Symbol);
Assert.Same(symbol, model.LookupSymbols(reference.SpanStart, name: decl.Identifier.ValueText).Single());
Assert.True(model.LookupNames(reference.SpanStart).Contains(decl.Identifier.ValueText));
}
}
示例7: VerifyModelForOutVarDuplicateInSameScope
private static void VerifyModelForOutVarDuplicateInSameScope(SemanticModel model, DeclarationExpressionSyntax decl)
{
var variableDesignationSyntax = GetVariableDesignation(decl);
var symbol = model.GetDeclaredSymbol(variableDesignationSyntax);
Assert.Equal(decl.Identifier().ValueText, symbol.Name);
Assert.Equal(variableDesignationSyntax, symbol.DeclaringSyntaxReferences.Single().GetSyntax());
Assert.Equal(LocalDeclarationKind.RegularVariable, ((LocalSymbol)symbol).DeclarationKind);
Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)variableDesignationSyntax));
Assert.NotEqual(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier().ValueText).Single());
Assert.True(model.LookupNames(decl.SpanStart).Contains(decl.Identifier().ValueText));
var local = (SourceLocalSymbol)symbol;
if (decl.Type().IsVar && local.IsVar && local.Type.IsErrorType())
{
Assert.Null(model.GetSymbolInfo(decl.Type()).Symbol);
}
else
{
Assert.Equal(local.Type, model.GetSymbolInfo(decl.Type()).Symbol);
}
}
示例8: VerifyModelForOutField
private static void VerifyModelForOutField(
SemanticModel model,
DeclarationExpressionSyntax decl,
bool duplicate,
params IdentifierNameSyntax[] references)
{
var variableDesignationSyntax = GetVariableDesignation(decl);
var symbol = model.GetDeclaredSymbol(variableDesignationSyntax);
Assert.Equal(decl.Identifier().ValueText, symbol.Name);
Assert.Equal(SymbolKind.Field, symbol.Kind);
Assert.Equal(variableDesignationSyntax, symbol.DeclaringSyntaxReferences.Single().GetSyntax());
Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)variableDesignationSyntax));
var symbols = model.LookupSymbols(decl.SpanStart, name: decl.Identifier().ValueText);
var names = model.LookupNames(decl.SpanStart);
if (duplicate)
{
Assert.True(symbols.Count() > 1);
Assert.Contains(symbol, symbols);
}
else
{
Assert.Same(symbol, symbols.Single());
}
Assert.Contains(decl.Identifier().ValueText, names);
var local = (FieldSymbol)symbol;
var typeSyntax = decl.Type();
Assert.True(SyntaxFacts.IsInNamespaceOrTypeContext(typeSyntax));
Assert.True(SyntaxFacts.IsInTypeOnlyContext(typeSyntax));
if (typeSyntax.IsVar && local.Type.IsErrorType())
{
Assert.Null(model.GetSymbolInfo(typeSyntax).Symbol);
}
else
{
Assert.Equal(local.Type, model.GetSymbolInfo(typeSyntax).Symbol);
}
var declarator = decl.Ancestors().OfType<VariableDeclaratorSyntax>().FirstOrDefault();
var inFieldDeclaratorArgumentlist = declarator != null && declarator.Parent.Parent.Kind() != SyntaxKind.LocalDeclarationStatement &&
(declarator.ArgumentList?.Contains(decl)).GetValueOrDefault();
if (inFieldDeclaratorArgumentlist)
{
Assert.Null(model.GetSymbolInfo(decl).Symbol);
Assert.Null(model.GetSymbolInfo(decl).Symbol);
}
else
{
Assert.Same(symbol, model.GetSymbolInfo(decl).Symbol);
Assert.Same(symbol, model.GetSymbolInfo(decl).Symbol);
}
Assert.Null(model.GetDeclaredSymbol(decl));
foreach (var reference in references)
{
var referenceInfo = model.GetSymbolInfo(reference);
symbols = model.LookupSymbols(reference.SpanStart, name: decl.Identifier().ValueText);
if (duplicate)
{
Assert.Null(referenceInfo.Symbol);
Assert.Contains(symbol, referenceInfo.CandidateSymbols);
Assert.True(symbols.Count() > 1);
Assert.Contains(symbol, symbols);
}
else
{
Assert.Same(symbol, referenceInfo.Symbol);
Assert.Same(symbol, symbols.Single());
Assert.Equal(local.Type, model.GetTypeInfo(reference).Type);
}
Assert.True(model.LookupNames(reference.SpanStart).Contains(decl.Identifier().ValueText));
}
if (!inFieldDeclaratorArgumentlist)
{
var dataFlowParent = (ExpressionSyntax)decl.Parent.Parent.Parent;
if (model.IsSpeculativeSemanticModel)
{
Assert.Throws<NotSupportedException>(() => model.AnalyzeDataFlow(dataFlowParent));
}
else
{
var dataFlow = model.AnalyzeDataFlow(dataFlowParent);
if (dataFlow.Succeeded)
{
Assert.False(dataFlow.VariablesDeclared.Contains(symbol, ReferenceEqualityComparer.Instance));
Assert.False(dataFlow.AlwaysAssigned.Contains(symbol, ReferenceEqualityComparer.Instance));
Assert.False(dataFlow.WrittenInside.Contains(symbol, ReferenceEqualityComparer.Instance));
Assert.False(dataFlow.DataFlowsIn.Contains(symbol, ReferenceEqualityComparer.Instance));
Assert.False(dataFlow.ReadInside.Contains(symbol, ReferenceEqualityComparer.Instance));
//.........这里部分代码省略.........
示例9: VerifyModelNotSupported
private static void VerifyModelNotSupported(
SemanticModel model,
DeclarationExpressionSyntax decl,
params IdentifierNameSyntax[] references)
{
var variableDeclaratorSyntax = GetVariableDesignation(decl);
Assert.Null(model.GetDeclaredSymbol(variableDeclaratorSyntax));
Assert.Null(model.GetDeclaredSymbol((SyntaxNode)variableDeclaratorSyntax));
var identifierText = decl.Identifier().ValueText;
Assert.False(model.LookupSymbols(decl.SpanStart, name: identifierText).Any());
Assert.False(model.LookupNames(decl.SpanStart).Contains(identifierText));
Assert.Null(model.GetSymbolInfo(decl.Type()).Symbol);
Assert.Null(model.GetSymbolInfo(decl).Symbol);
Assert.Null(model.GetTypeInfo(decl).Type);
Assert.Null(model.GetDeclaredSymbol(decl));
VerifyModelNotSupported(model, references);
}
示例10: GetUnqualifiedSymbols
private static IEnumerable<ISymbol> GetUnqualifiedSymbols(SyntaxToken token, SemanticModel semanticModel, CancellationToken cancellationToken)
{
foreach (var symbol in semanticModel.LookupSymbols(token.SpanStart))
{
yield return symbol;
}
// LookupSymbols doesn't return indexers or operators because they can't be referred to by name.
// So, try to find the innermost type declaration and return its operators and indexers
var typeDeclaration = token.Parent?.FirstAncestorOrSelf<TypeDeclarationSyntax>();
if (typeDeclaration != null)
{
var type = semanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken);
if (type != null)
{
foreach (var baseType in type.GetBaseTypesAndThis())
{
foreach (var member in baseType.GetMembers())
{
if ((member.IsIndexer() || member.IsUserDefinedOperator()) &&
member.IsAccessibleWithin(type))
{
yield return member;
}
}
}
}
}
}
示例11: CheckSymbols
/// <summary>
/// Assert that the result of LookupNames(position) matches the list of expected names.
/// </summary>
private static void CheckSymbols(SemanticModel model, int keyPositionNum, int position, IEnumerable<string> expectedSymbols)
{
var actualSymbols = model.LookupSymbols(position).Select(SymbolUtilities.ToTestDisplayString).ToArray();
Array.Sort(actualSymbols);
SyntaxToken token = model.SyntaxTree.GetCompilationUnitRoot().FindToken(position, findInsideTrivia: true);
AssertEx.Equal(expectedSymbols, actualSymbols,
message: string.Format("Lookup({0}) - '{1}' in '{2}' after {3}th '{4}' - \"-->\" found but not expected, \"++>\" expected but not found",
position, token.ToString(), token.Parent.ToString(), keyPositionNum, KeyPositionMarker));
}
示例12: VerifyModelForDeclarationPattern
private static void VerifyModelForDeclarationPattern(SemanticModel model, DeclarationPatternSyntax decl, bool isShadowed, params IdentifierNameSyntax[] references)
{
var symbol = model.GetDeclaredSymbol(decl);
Assert.Equal(decl.Identifier.ValueText, symbol.Name);
Assert.Equal(LocalDeclarationKind.PatternVariable, ((LocalSymbol)symbol).DeclarationKind);
Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)decl));
if (isShadowed)
{
Assert.NotEqual(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single());
}
else
{
Assert.Same(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single());
}
Assert.True(model.LookupNames(decl.SpanStart).Contains(decl.Identifier.ValueText));
var type = ((LocalSymbol)symbol).Type;
if (!decl.Type.IsVar || !type.IsErrorType())
{
Assert.Equal(type, model.GetSymbolInfo(decl.Type).Symbol);
}
foreach (var reference in references)
{
Assert.Same(symbol, model.GetSymbolInfo(reference).Symbol);
Assert.Same(symbol, model.LookupSymbols(reference.SpanStart, name: decl.Identifier.ValueText).Single());
Assert.True(model.LookupNames(reference.SpanStart).Contains(decl.Identifier.ValueText));
}
}
示例13: VerifyModelForOutVar
private static void VerifyModelForOutVar(SemanticModel model, ArgumentSyntax decl, bool isDelegateCreation, bool isExecutableCode, params IdentifierNameSyntax[] references)
{
var variableDeclaratorSyntax = GetVariableDeclarator(decl);
var symbol = model.GetDeclaredSymbol(variableDeclaratorSyntax);
Assert.Equal(decl.Identifier.ValueText, symbol.Name);
Assert.Equal(LocalDeclarationKind.RegularVariable, ((LocalSymbol)symbol).DeclarationKind);
Assert.Same(symbol, model.GetDeclaredSymbol((SyntaxNode)variableDeclaratorSyntax));
Assert.Same(symbol, model.LookupSymbols(decl.SpanStart, name: decl.Identifier.ValueText).Single());
Assert.True(model.LookupNames(decl.SpanStart).Contains(decl.Identifier.ValueText));
var local = (SourceLocalSymbol)symbol;
if (decl.Type.IsVar && local.IsVar && local.Type.IsErrorType())
{
Assert.Null(model.GetSymbolInfo(decl.Type).Symbol);
}
else
{
Assert.Equal(local.Type, model.GetSymbolInfo(decl.Type).Symbol);
}
foreach (var reference in references)
{
Assert.Same(symbol, model.GetSymbolInfo(reference).Symbol);
Assert.Same(symbol, model.LookupSymbols(reference.SpanStart, name: decl.Identifier.ValueText).Single());
Assert.True(model.LookupNames(reference.SpanStart).Contains(decl.Identifier.ValueText));
Assert.Equal(local.Type, model.GetTypeInfo(reference).Type);
}
VerifyDataFlow(model, decl, isDelegateCreation, isExecutableCode, references, symbol);
}
示例14: GetQualifiedSymbols
private static ImmutableArray<ISymbol> GetQualifiedSymbols(
QualifiedCrefSyntax parent, SyntaxToken token, SemanticModel semanticModel, CancellationToken cancellationToken)
{
var leftType = semanticModel.GetTypeInfo(parent.Container, cancellationToken).Type;
var leftSymbol = semanticModel.GetSymbolInfo(parent.Container, cancellationToken).Symbol;
var container = (leftSymbol ?? leftType) as INamespaceOrTypeSymbol;
var result = ArrayBuilder<ISymbol>.GetInstance();
result.AddRange(semanticModel.LookupSymbols(token.SpanStart, container));
var namedTypeContainer = container as INamedTypeSymbol;
if (namedTypeContainer != null)
{
result.AddRange(namedTypeContainer.InstanceConstructors);
}
return result.ToImmutableAndFree();
}
示例15: GetUnqualifiedSymbols
private static ImmutableArray<ISymbol> GetUnqualifiedSymbols(
SyntaxToken token, SemanticModel semanticModel, CancellationToken cancellationToken)
{
var result = ArrayBuilder<ISymbol>.GetInstance();
result.AddRange(semanticModel.LookupSymbols(token.SpanStart));
// LookupSymbols doesn't return indexers or operators because they can't be referred to by name.
// So, try to find the innermost type declaration and return its operators and indexers
var typeDeclaration = token.Parent?.FirstAncestorOrSelf<TypeDeclarationSyntax>();
if (typeDeclaration != null)
{
var type = semanticModel.GetDeclaredSymbol(typeDeclaration, cancellationToken);
if (type != null)
{
foreach (var baseType in type.GetBaseTypesAndThis())
{
foreach (var member in baseType.GetMembers())
{
if ((member.IsIndexer() || member.IsUserDefinedOperator()) &&
member.IsAccessibleWithin(type))
{
result.Add(member);
}
}
}
}
}
return result.ToImmutableAndFree();
}