public static AstType CreateShortType(BaseRefactoringContext refactoringContext, IType expressionType, AstNode node) { var csResolver = refactoringContext.Resolver.GetResolverStateBefore(node); var builder = new TypeSystemAstBuilder(csResolver); return(builder.ConvertType(expressionType)); }
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 issueText = ctx.TranslateString("Collection element type '{0}' is not implicitly convertible to '{1}'"); string fixText = ctx.TranslateString("Use type '{0}'"); AddIssue(variableType, string.Format(issueText, elementType.GetText(), variableType.GetText()), new CodeAction(string.Format(fixText, elementType.GetText()), script => script.Replace(variableType, elementType))); }
IEnumerable <CodeAction> GetActions(ParameterDeclaration parameter, IEnumerable <IType> possibleTypes) { var csResolver = ctx.Resolver.GetResolverStateBefore(parameter); var astBuilder = new TypeSystemAstBuilder(csResolver); foreach (var type in possibleTypes) { var localType = type; var message = String.Format(ctx.TranslateString("Demote parameter to '{0}'"), type.FullName); yield return(new CodeAction(message, script => { script.Replace(parameter.Type, astBuilder.ConvertType(localType)); }, parameter.NameToken)); } }
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 issueText = ctx.TranslateString("Collection element type '{0}' is not implicitly convertible to '{1}'"); string fixText = ctx.TranslateString("Use type '{0}'"); AddIssue(variableType, string.Format(issueText, elementType.GetText(), variableType.GetText()), new CodeAction( string.Format(fixText, elementType.GetText()), script => script.Replace(variableType, elementType), foreachStatement)); }
IEnumerable<CodeAction> GetActions(ParameterDeclaration parameter, IEnumerable<IType> possibleTypes) { var csResolver = ctx.Resolver.GetResolverStateBefore(parameter); var astBuilder = new TypeSystemAstBuilder(csResolver); foreach (var type in possibleTypes) { var localType = type; var message = string.Format(ctx.TranslateString("Demote parameter to '{0}'"), type.FullName); yield return new CodeAction(message, script => { script.Replace(parameter.Type, astBuilder.ConvertType(localType)); }); } }