本文整理汇总了C#中SemanticModel.GetEnclosingSymbol方法的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel.GetEnclosingSymbol方法的具体用法?C# SemanticModel.GetEnclosingSymbol怎么用?C# SemanticModel.GetEnclosingSymbol使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类SemanticModel
的用法示例。
在下文中一共展示了SemanticModel.GetEnclosingSymbol方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: GetEnclosingMethodOrPropertyOrField
private static ISymbol GetEnclosingMethodOrPropertyOrField(
SemanticModel semanticModel,
ReferenceLocation reference)
{
var enclosingSymbol = semanticModel.GetEnclosingSymbol(reference.Location.SourceSpan.Start);
for (var current = enclosingSymbol; current != null; current = current.ContainingSymbol)
{
if (current.Kind == SymbolKind.Field)
{
return current;
}
if (current.Kind == SymbolKind.Property)
{
return current;
}
if (current.Kind == SymbolKind.Method)
{
var method = (IMethodSymbol)current;
if (method.IsAccessor())
{
return method.AssociatedSymbol;
}
if (method.MethodKind != MethodKind.AnonymousFunction)
{
return method;
}
}
}
return null;
}
示例2: TryGetMethodReturnType
private bool TryGetMethodReturnType(SyntaxNode node, SemanticModel model, CancellationToken cancellationToken, out ITypeSymbol methodReturnType)
{
methodReturnType = null;
var symbol = model.GetEnclosingSymbol(node.Span.Start, cancellationToken);
var method = symbol as IMethodSymbol;
if (method == null || method.ReturnsVoid)
{
return false;
}
methodReturnType = method.ReturnType;
return methodReturnType != null;
}
示例3: TrySimplifyMemberAccessOrQualifiedName
// Note: The caller needs to verify that replacement doesn't change semantics of the original expression.
private static bool TrySimplifyMemberAccessOrQualifiedName(
ExpressionSyntax left,
ExpressionSyntax right,
SemanticModel semanticModel,
OptionSet optionSet,
out ExpressionSyntax replacementNode,
out TextSpan issueSpan)
{
replacementNode = null;
issueSpan = default(TextSpan);
if (left != null && right != null)
{
var leftSymbol = SimplificationHelpers.GetOriginalSymbolInfo(semanticModel, left);
if (leftSymbol != null && (leftSymbol.Kind == SymbolKind.NamedType))
{
var rightSymbol = SimplificationHelpers.GetOriginalSymbolInfo(semanticModel, right);
if (rightSymbol != null && (rightSymbol.IsStatic || rightSymbol.Kind == SymbolKind.NamedType))
{
// Static member access or nested type member access.
INamedTypeSymbol containingType = rightSymbol.ContainingType;
var enclosingSymbol = semanticModel.GetEnclosingSymbol(left.SpanStart);
List<ISymbol> enclosingTypeParametersInsideOut = new List<ISymbol>();
while (enclosingSymbol != null)
{
if (enclosingSymbol is IMethodSymbol)
{
var methodSymbol = (IMethodSymbol)enclosingSymbol;
if (methodSymbol.TypeArguments.Length != 0)
{
enclosingTypeParametersInsideOut.AddRange(methodSymbol.TypeArguments);
}
}
if (enclosingSymbol is INamedTypeSymbol)
{
var namedTypeSymbol = (INamedTypeSymbol)enclosingSymbol;
if (namedTypeSymbol.TypeArguments.Length != 0)
{
enclosingTypeParametersInsideOut.AddRange(namedTypeSymbol.TypeArguments);
}
}
enclosingSymbol = enclosingSymbol.ContainingSymbol;
}
if (containingType != null && !containingType.Equals(leftSymbol))
{
var namedType = leftSymbol as INamedTypeSymbol;
if (namedType != null)
{
if ((namedType.GetBaseTypes().Contains(containingType) &&
!optionSet.GetOption(SimplificationOptions.AllowSimplificationToBaseType)) ||
(!optionSet.GetOption(SimplificationOptions.AllowSimplificationToGenericType) &&
containingType.TypeArguments.Length != 0))
{
return false;
}
}
// We have a static member access or a nested type member access using a more derived type.
// Simplify syntax so as to use accessed member's most immediate containing type instead of the derived type.
replacementNode = containingType.GenerateTypeSyntax()
.WithLeadingTrivia(left.GetLeadingTrivia())
.WithTrailingTrivia(left.GetTrailingTrivia());
issueSpan = left.Span;
return true;
}
}
}
}
return false;
}
示例4: TryDetermineOverridableMembers
private bool TryDetermineOverridableMembers(
SemanticModel semanticModel, SyntaxToken startToken, Accessibility seenAccessibility, out ISet<ISymbol> overridableMembers)
{
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);
// Retain overridable methods
AddOverridableMembers(result, containingType, type);
}
// Don't suggest already overridden members
RemoveOverriddenMembers(result, containingType);
}
}
// Filter based on accessibility
if (seenAccessibility != Accessibility.NotApplicable)
{
result.RemoveWhere(m => m.DeclaredAccessibility != seenAccessibility);
}
overridableMembers = result;
return overridableMembers.Count > 0;
}