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


C# ITypeSymbol.GenerateTypeSyntax方法代码示例

本文整理汇总了C#中ITypeSymbol.GenerateTypeSyntax方法的典型用法代码示例。如果您正苦于以下问题:C# ITypeSymbol.GenerateTypeSyntax方法的具体用法?C# ITypeSymbol.GenerateTypeSyntax怎么用?C# ITypeSymbol.GenerateTypeSyntax使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在ITypeSymbol的用法示例。


在下文中一共展示了ITypeSymbol.GenerateTypeSyntax方法的9个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: FixMethod

        private SyntaxNode FixMethod(
            bool keepVoid, IMethodSymbol methodSymbol, MethodDeclarationSyntax method,
            ITypeSymbol taskType, INamedTypeSymbol taskOfTType)
        {
            var newReturnType = method.ReturnType;

            if (methodSymbol.ReturnsVoid)
            {
                if (!keepVoid)
                {
                    newReturnType = taskType.GenerateTypeSyntax();
                }
            }
            else
            {
                if (!IsTaskLike(methodSymbol.ReturnType, taskType, taskOfTType))
                {
                    // If it's not already Task-like, then wrap the existing return type
                    // in Task<>.
                    newReturnType = taskOfTType.Construct(methodSymbol.ReturnType).GenerateTypeSyntax();
                }
            }

            var newModifiers = method.Modifiers.Add(s_asyncToken);
            return method.WithReturnType(newReturnType).WithModifiers(newModifiers);
        }
开发者ID:tvsonar,项目名称:roslyn,代码行数:26,代码来源:CSharpMakeMethodAsynchronousCodeFixProvider.cs

示例2: Cast

 public static CastExpressionSyntax Cast(
     this ExpressionSyntax expression,
     ITypeSymbol targetType)
 {
     return SyntaxFactory.CastExpression(
         type: targetType.GenerateTypeSyntax(),
         expression: expression.Parenthesize())
         .WithAdditionalAnnotations(Simplifier.Annotation);
 }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:9,代码来源:ExpressionSyntaxExtensions.cs

示例3: CastIfPossible

        /// <summary>
        /// Adds to <paramref name="targetType"/> if it does not contain an anonymous
        /// type and binds to the same type at the given <paramref name="position"/>.
        /// </summary>
        public static ExpressionSyntax CastIfPossible(
            this ExpressionSyntax expression,
            ITypeSymbol targetType,
            int position,
            SemanticModel semanticModel,
            out bool wasCastAdded)
        {
            wasCastAdded = false;

            if (targetType.ContainsAnonymousType())
            {
                return expression;
            }

            if (targetType.Kind == SymbolKind.DynamicType)
            {
                targetType = semanticModel.Compilation.GetSpecialType(SpecialType.System_Object);
            }

            var typeSyntax = targetType.GenerateTypeSyntax();
            var type = semanticModel.GetSpeculativeTypeInfo(
                position,
                typeSyntax,
                SpeculativeBindingOption.BindAsTypeOrNamespace).Type;

            if (!targetType.Equals(type))
            {
                return expression;
            }

            var castExpression = expression.Cast(targetType);

            // Ensure that inserting the cast doesn't change the semantics.
            var specAnalyzer = new SpeculationAnalyzer(expression, castExpression, semanticModel, CancellationToken.None);
            var speculativeSemanticModel = specAnalyzer.SpeculativeSemanticModel;
            if (speculativeSemanticModel == null)
            {
                return expression;
            }

            var speculatedCastExpression = (CastExpressionSyntax)specAnalyzer.ReplacedExpression;
            if (!speculatedCastExpression.IsUnnecessaryCast(speculativeSemanticModel, CancellationToken.None))
            {
                return expression;
            }

            wasCastAdded = true;
            return castExpression;
        }
开发者ID:nileshjagtap,项目名称:roslyn,代码行数:53,代码来源:ExpressionSyntaxExtensions.cs

示例4: GetTypeReferenceString

		public string GetTypeReferenceString (ITypeSymbol type, bool highlight = true)
		{
			if (type == null)
				throw new ArgumentNullException (nameof (type));
			if (type.TypeKind == TypeKind.Error) {
				var typeSyntax = type.GenerateTypeSyntax ();
				string generatedTypeSyntaxString;
				try {
					var oldDoc = ctx.AnalysisDocument;
					var newDoc = oldDoc.WithSyntaxRoot (SyntaxFactory.ParseCompilationUnit (typeSyntax.ToString ()).WithAdditionalAnnotations (Simplifier.Annotation));
					var reducedDoc = Simplifier.ReduceAsync (newDoc, options);
					generatedTypeSyntaxString = Ambience.EscapeText (reducedDoc.Result.GetSyntaxRootAsync ().Result.ToString ());
				} catch {
					generatedTypeSyntaxString = typeSyntax != null ? Ambience.EscapeText (typeSyntax.ToString ()) : "?";
				}
				return highlight ? HighlightSemantically (generatedTypeSyntaxString, colorStyle.UserTypes) : generatedTypeSyntaxString;
			}
			if (type.TypeKind == TypeKind.Array) {
				var arrayType = (IArrayTypeSymbol)type;
				return GetTypeReferenceString (arrayType.ElementType, highlight) + "[" + new string (',', arrayType.Rank - 1) + "]";
			}
			if (type.TypeKind == TypeKind.Pointer)
				return GetTypeReferenceString (((IPointerTypeSymbol)type).PointedAtType, highlight) + "*";
			string displayString;
			if (ctx != null) {
				SemanticModel model = SemanticModel;
				if (model == null) {
					var parsedDocument = ctx.ParsedDocument;
					if (parsedDocument != null) {
						model = parsedDocument.GetAst<SemanticModel> () ?? ctx.AnalysisDocument?.GetSemanticModelAsync ().Result;
					}
				}
				//Math.Min (model.SyntaxTree.Length, offset)) is needed in case parsedDocument.GetAst<SemanticModel> () is outdated
				//this is tradeoff between performance and consistency between editor text(offset) and model, since
				//ToMinimalDisplayString can use little outdated model this is fine
				//but in case of Sketches where user usually is at end of document when typing text this can throw exception
				//because offset can be >= Length
				displayString = model != null ? RoslynCompletionData.SafeMinimalDisplayString (type, model, Math.Min (model.SyntaxTree.Length - 1, offset), MonoDevelop.Ide.TypeSystem.Ambience.LabelFormat) : type.Name;
			} else {
				displayString = type.ToDisplayString (MonoDevelop.Ide.TypeSystem.Ambience.LabelFormat);
			}
			var text = MonoDevelop.Ide.TypeSystem.Ambience.EscapeText (displayString);
			return highlight ? HighlightSemantically (text, colorStyle.UserTypes) : text;
		}
开发者ID:kdubau,项目名称:monodevelop,代码行数:44,代码来源:SignatureMarkupCreator.cs

示例5: CreateDefaultExpression

        public override SyntaxNode CreateDefaultExpression(ITypeSymbol type)
        {
            // If it's just a reference type, then "null" is the default expression for it.  Note:
            // this counts for actual reference type, or a type parameter with a 'class' constraint.
            // Also, if it's a nullable type, then we can use "null".
            if (type.IsReferenceType ||
                type.IsPointerType() ||
                type.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T)
            {
                return SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression);
            }

            switch (type.SpecialType)
            {
                case SpecialType.System_Boolean:
                    return SyntaxFactory.LiteralExpression(SyntaxKind.FalseLiteralExpression);
                case SpecialType.System_SByte:
                case SpecialType.System_Byte:
                case SpecialType.System_Int16:
                case SpecialType.System_UInt16:
                case SpecialType.System_Int32:
                case SpecialType.System_UInt32:
                case SpecialType.System_Int64:
                case SpecialType.System_UInt64:
                case SpecialType.System_Decimal:
                case SpecialType.System_Single:
                case SpecialType.System_Double:
                    return SyntaxFactory.LiteralExpression(
                        SyntaxKind.NumericLiteralExpression, SyntaxFactory.Literal("0", 0));
            }

            // Default to a "default(<typename>)" expression.
            return SyntaxFactory.DefaultExpression(type.GenerateTypeSyntax());
        }
开发者ID:nagyist,项目名称:roslyn,代码行数:34,代码来源:CSharpSyntaxFactory.cs

示例6: CreateObjectCreationExpression

 public override SyntaxNode CreateObjectCreationExpression(ITypeSymbol typeName, IList<SyntaxNode> arguments)
 {
     return SyntaxFactory.ObjectCreationExpression(typeName.GenerateTypeSyntax(), CreateArgumentList(arguments), null);
 }
开发者ID:nagyist,项目名称:roslyn,代码行数:4,代码来源:CSharpSyntaxFactory.cs

示例7: CreateLocalDeclarationStatement

 public override SyntaxNode CreateLocalDeclarationStatement(bool isConst, ITypeSymbol type, SyntaxNode variableDeclarator)
 {
     return SyntaxFactory.LocalDeclarationStatement(
         isConst ? SyntaxFactory.TokenList(SyntaxFactory.Token(SyntaxKind.ConstKeyword)) : default(SyntaxTokenList),
         SyntaxFactory.VariableDeclaration(
             type == null ? SyntaxFactory.IdentifierName("var") : type.GenerateTypeSyntax(),
             SyntaxFactory.SingletonSeparatedList((VariableDeclaratorSyntax)variableDeclarator)));
 }
开发者ID:nagyist,项目名称:roslyn,代码行数:8,代码来源:CSharpSyntaxFactory.cs

示例8: CreateConvertExpression

 public override SyntaxNode CreateConvertExpression(ITypeSymbol type, SyntaxNode expression)
 {
     return SyntaxFactory.CastExpression(type.GenerateTypeSyntax(), Parenthesize(expression));
 }
开发者ID:nagyist,项目名称:roslyn,代码行数:4,代码来源:CSharpSyntaxFactory.cs

示例9: CreateAsExpression

 public override SyntaxNode CreateAsExpression(SyntaxNode expression, ITypeSymbol type)
 {
     return SyntaxFactory.BinaryExpression(SyntaxKind.AsExpression, Parenthesize(expression), type.GenerateTypeSyntax());
 }
开发者ID:nagyist,项目名称:roslyn,代码行数:4,代码来源:CSharpSyntaxFactory.cs


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