當前位置: 首頁>>代碼示例>>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;未經允許,請勿轉載。