本文整理汇总了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;
}
示例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));
}