当前位置: 首页>>代码示例>>C#>>正文


C# CodeAnalysis.SemanticModel类代码示例

本文整理汇总了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;
		}
开发者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


注:本文中的Microsoft.CodeAnalysis.SemanticModel类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。