public override void VisitForeachStatement(ForeachStatement foreachStatement) { base.VisitForeachStatement(foreachStatement); var rr = ctx.Resolve(foreachStatement) as ForEachResolveResult; if (rr == null) { return; } if (rr.ElementType.Kind == TypeKind.Unknown) { return; } if (ReflectionHelper.GetTypeCode(rr.ElementType) == TypeCode.Object) { return; } if (conversions == null) { conversions = CSharpConversions.Get(ctx.Compilation); } Conversion c = conversions.ImplicitConversion(rr.ElementType, rr.ElementVariable.Type); if (c.IsValid) { return; } var csResolver = ctx.GetResolverStateBefore(foreachStatement); var builder = new TypeSystemAstBuilder(csResolver); AstType elementType = builder.ConvertType(rr.ElementType); AstType variableType = foreachStatement.VariableType; string text = ctx.TranslateString("Collection element type '{0}' is not implicitly convertible to '{1}'"); AddIssue(variableType, string.Format(text, elementType.GetText(), variableType.GetText())); }
CodeAction ReplaceWithFullTypeNameAction(RefactoringContext context, AstNode node, ITypeDefinition typeDefinition) { AstType astType = context.CreateShortType(typeDefinition); string textWithoutGenerics = astType.GetText(); foreach (var typeArg in node.GetChildrenByRole(Roles.TypeArgument)) { astType.AddChild(typeArg.Clone(), Roles.TypeArgument); } return(new CodeAction(textWithoutGenerics, s => s.Replace(node, astType))); }
public string ConvertType(IType type) { if (type == null) { throw new ArgumentNullException("type"); } TypeSystemAstBuilder astBuilder = CreateAstBuilder(); AstType astType = astBuilder.ConvertType(type); return(astType.GetText()); }