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


C# ITypeSymbol.ContainsAnonymousType方法代码示例

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


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

示例1: 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)
        {
            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;
            }

            return castExpression;
        }
开发者ID:RoryVL,项目名称:roslyn,代码行数:49,代码来源:ExpressionSyntaxExtensions.cs

示例2: GetPreselectedSymbolsWorker2

		static Task<IEnumerable<ISymbol>> GetPreselectedSymbolsWorker2 (CSharpSyntaxContext context, ITypeSymbol type, CancellationToken cancellationToken)
		{
			// Unwrap an array type fully.  We only want to offer the underlying element type in the
			// list of completion items.
			bool isArray = false;
			while (type is IArrayTypeSymbol) {
				isArray = true;
				type = ((IArrayTypeSymbol)type).ElementType;
			}

			if (type == null) {
				return Task.FromResult (Enumerable.Empty<ISymbol> ());
			}

			// Unwrap nullable
			if (type.OriginalDefinition.SpecialType == SpecialType.System_Nullable_T) {
				type = type.GetTypeArguments ().FirstOrDefault ();
			}

			if (type.SpecialType == SpecialType.System_Void) {
				return Task.FromResult (Enumerable.Empty<ISymbol> ());
			}

			if (type.ContainsAnonymousType ()) {
				return Task.FromResult (Enumerable.Empty<ISymbol> ());
			}

			if (!type.CanBeReferencedByName) {
				return Task.FromResult (Enumerable.Empty<ISymbol> ());
			}

			// Normally the user can't say things like "new IList".  Except for "IList[] x = new |".
			// In this case we do want to allow them to preselect certain types in the completion
			// list even if they can't new them directly.
			if (!isArray) {
				if (type.TypeKind == TypeKind.Interface ||
				    type.TypeKind == TypeKind.Pointer ||
				    type.TypeKind == TypeKind.Dynamic ||
				    type.IsAbstract) {
					return Task.FromResult (Enumerable.Empty<ISymbol> ());
				}

				if (type.TypeKind == TypeKind.TypeParameter &&
				    !((ITypeParameterSymbol)type).HasConstructorConstraint) {
					return Task.FromResult (Enumerable.Empty<ISymbol> ());
				}
			}

//			if (!type.IsEditorBrowsable(options.GetOption(RecommendationOptions.HideAdvancedMembers, context.SemanticModel.Language), context.SemanticModel.Compilation))
//			{
//				return SpecializedTasks.EmptyEnumerable<ISymbol>();
//			}
//
			return Task.FromResult (SpecializedCollections.SingletonEnumerable ((ISymbol)type));
		}
开发者ID:powerumc,项目名称:monodevelop_korean,代码行数:55,代码来源:ObjectCreationContextHandler.cs


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