本文整理汇总了C#中Microsoft.CodeAnalysis.SemanticModel类的典型用法代码示例。如果您正苦于以下问题:C# SemanticModel类的具体用法?C# SemanticModel怎么用?C# SemanticModel使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
SemanticModel类属于Microsoft.CodeAnalysis命名空间,在下文中一共展示了SemanticModel类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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;
}
开发者ID:sushihangover,项目名称:monodevelop,代码行数:28,代码来源:CSharpImplementAbstractClassService.cs
示例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;
}
开发者ID:pabloescribanoloza,项目名称:monodevelop,代码行数:33,代码来源:ProtocolMemberContextHandler.cs
示例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;
}
开发者ID:Rickinio,项目名称:roslyn,代码行数:26,代码来源:DeclarationComputer.cs
示例5: MemberMetricsCalculator
public MemberMetricsCalculator(SemanticModel semanticModel, Solution solution, IAsyncFactory<ISymbol, IMemberDocumentation> documentationFactory)
: base(semanticModel)
{
_solution = solution;
_documentationFactory = documentationFactory;
_nameResolver = new MemberNameResolver(Model);
}
开发者ID:jjrdk,项目名称:ArchiMetrics,代码行数:7,代码来源:MemberMetricsCalculator.cs
示例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;
}
开发者ID:ozgurkayaist,项目名称:sonarlint-vs,代码行数:26,代码来源:ThreadStaticNonStaticField.cs
示例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));
}
开发者ID:maggiemsft,项目名称:roslyn-analyzers,代码行数:7,代码来源:UseOrdinalStringComparison.Fixer.cs
示例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);
}
开发者ID:GloryChou,项目名称:roslyn,代码行数:7,代码来源:UseOrdinalStringComparison.cs
示例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;
}
开发者ID:JeanLLopes,项目名称:code-cracker,代码行数:25,代码来源:DisposableVariableNotDisposedCodeFixProvider.cs
示例10: DiscoveryWalker
public DiscoveryWalker(int start, int end, SemanticModel model)
{
_model = model;
_start = start;
_end = end;
DefinedOutside = new Dictionary<string, string>();
}
开发者ID:AmadeusW,项目名称:SnippetVS,代码行数:7,代码来源:DiscoveryWalker.cs
示例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;
}
开发者ID:FreeBSD-DotNet,项目名称:monodevelop,代码行数:33,代码来源:CSharpAddAsyncCodeFixProvider.cs
示例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);
}
开发者ID:SergeyTeplyakov,项目名称:ErrorProne.NET,代码行数:7,代码来源:PureMethodExtensions.cs
示例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);
}
开发者ID:SergeyTeplyakov,项目名称:ErrorProne.NET,代码行数:7,代码来源:PureMethodExtensions.cs
示例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();
}
开发者ID:hexuefengx,项目名称:StyleCopAnalyzers,代码行数:31,代码来源:SA1121CodeFixProvider.cs
示例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);
}
开发者ID:henrylle,项目名称:ArchiMetrics,代码行数:28,代码来源:DoNotCallOverridableMembersInConstructorRule.cs
示例16: StatMachineGeneratorFixer
public StatMachineGeneratorFixer(Compilation compilation, SyntaxTree syntaxTree, SemanticModel semanticModel, string enclosingTypeName)
{
this.compilation = compilation;
this.syntaxTree = syntaxTree;
this.semanticModel = semanticModel;
this.enclosingTypeName = enclosingTypeName;
}
开发者ID:mortezabarzkar,项目名称:SharpNative,代码行数:7,代码来源:StatMachineGeneratorFixer.cs
示例17: FindApplicableAlias
public static async Task<ISymbol> FindApplicableAlias(this ITypeSymbol type, int position, SemanticModel semanticModel, CancellationToken cancellationToken)
{
try
{
if (semanticModel.IsSpeculativeSemanticModel)
{
position = semanticModel.OriginalPositionForSpeculation;
semanticModel = semanticModel.ParentModel;
}
var root = await semanticModel.SyntaxTree.GetRootAsync(cancellationToken).ConfigureAwait(false);
IEnumerable<UsingDirectiveSyntax> applicableUsings = GetApplicableUsings(position, root as CompilationUnitSyntax);
foreach (var applicableUsing in applicableUsings)
{
var alias = semanticModel.GetOriginalSemanticModel().GetDeclaredSymbol(applicableUsing, cancellationToken);
if (alias != null && alias.Target == type)
{
return alias;
}
}
return null;
}
catch (Exception e) when (FatalError.ReportUnlessCanceled(e))
{
throw ExceptionUtilities.Unreachable;
}
}
开发者ID:Rickinio,项目名称:roslyn,代码行数:29,代码来源:ITypeSymbolExtensions.cs
示例18: FindReoccuringOperations
private List<KeyValuePair<BinaryExpressionSyntax, BinaryExpressionSyntax>> FindReoccuringOperations(
IEnumerable<BinaryExpressionSyntax> operations, SemanticModel semanticModel)
{
var originalList = operations.ToList();
var copiedList = originalList.ToList();
var duplicates = new List<KeyValuePair<BinaryExpressionSyntax, BinaryExpressionSyntax>>();
var alreadyFound = new HashSet<BinaryExpressionSyntax>();
foreach (var oItem in originalList)
{
foreach (var cItem in copiedList)
{
if (alreadyFound.Contains(oItem) && alreadyFound.Contains(cItem)) continue;
if (IsEqualButNotTheSame(oItem, cItem, semanticModel))
{
alreadyFound.Add(oItem);
alreadyFound.Add(cItem);
duplicates.Add(new KeyValuePair<BinaryExpressionSyntax, BinaryExpressionSyntax>(oItem, cItem));
break;
}
}
}
return duplicates;
}
开发者ID:birksimon,项目名称:CodeAnalysis,代码行数:26,代码来源:LimitConditionInspector.cs
示例19: HasFlagsAttribute
internal static bool HasFlagsAttribute(SyntaxNode node, SemanticModel semanticModel)
{
var symbol = semanticModel.GetDeclaredSymbol(node);
return symbol != null &&
symbol.GetAttributes().Any(attribute => attribute.AttributeClass.Is(KnownType.System_FlagsAttribute));
}
开发者ID:dbolkensteyn,项目名称:sonarlint-vs,代码行数:7,代码来源:FlagsEnumWithoutInitializerBase.cs
示例20: ConvertParameters
static IEnumerable<ParameterSyntax> ConvertParameters(SemanticModel model, SyntaxNode lambda, IEnumerable<ParameterSyntax> list)
{
ITypeSymbol type = null;
int i = 0;
foreach (var param in list)
{
if (param.Type != null)
{
yield return param;
}
else
{
if (type == null)
{
var typeInfo = model.GetTypeInfo(lambda);
type = typeInfo.ConvertedType ?? typeInfo.Type;
if (type == null || !type.IsDelegateType())
yield break;
}
yield return SyntaxFactory.Parameter(
param.AttributeLists,
param.Modifiers,
SyntaxFactory.ParseTypeName(type.GetDelegateInvokeMethod().Parameters[i].Type.ToMinimalDisplayString(model, lambda.SpanStart)),
param.Identifier,
null
);
}
i++;
}
}
开发者ID:alecor191,项目名称:RefactoringEssentials,代码行数:31,代码来源:ConvertLambdaToAnonymousMethodCodeRefactoringProvider.cs
注:本文中的Microsoft.CodeAnalysis.SemanticModel类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论