Beispiel #1
0
            void AnalyzeExpression(AstNode expression, AstNode body, AstNodeCollection <ParameterDeclaration> parameters)
            {
                var invocation = AnalyzeBody(body);

                if (invocation == null)
                {
                    return;
                }
                if (!IsSimpleTarget(invocation.Target))
                {
                    return;
                }
                var lambdaParameters = parameters.ToList();

                if (lambdaParameters.Count != invocation.Arguments.Count)
                {
                    return;
                }
                int i = 0;

                foreach (var param in invocation.Arguments)
                {
                    var id = param as IdentifierExpression;
                    if (id == null)
                    {
                        return;
                    }
                    if (lambdaParameters[i].Name != id.Identifier)
                    {
                        return;
                    }
                    i++;
                }
                AddIssue(expression, ctx.TranslateString("Expression can be reduced to delegate"), script => {
                    var validTypes = CreateFieldAction.GetValidTypes(ctx.Resolver, expression).ToList();
                    if (validTypes.Any(t => t.FullName == "System.Func" && t.TypeParameterCount == 1 + parameters.Count) && validTypes.Any(t => t.FullName == "System.Action"))
                    {
                        var rr = ctx.Resolve(invocation) as CSharpInvocationResolveResult;
                        if (rr != null && rr.Member.ReturnType.Kind != ICSharpCode.NRefactory.TypeSystem.TypeKind.Void)
                        {
                            var builder = ctx.CreateTypeSytemAstBuilder(expression);
                            var type    = builder.ConvertType(new TopLevelTypeName("System", "Func", 1));
                            var args    = type is SimpleType ? ((SimpleType)type).TypeArguments : ((MemberType)type).TypeArguments;
                            args.Clear();
                            foreach (var pde in parameters)
                            {
                                args.Add(builder.ConvertType(ctx.Resolve(pde).Type));
                            }
                            args.Add(builder.ConvertType(rr.Member.ReturnType));
                            script.Replace(expression, new CastExpression(type, invocation.Target.Clone()));
                            return;
                        }
                    }
                    script.Replace(expression, invocation.Target.Clone());
                });
            }
            void AnalyzeExpression(AstNode expression, AstNode body, AstNodeCollection <ParameterDeclaration> parameters)
            {
                var invocation = AnalyzeBody(body);

                if (invocation == null)
                {
                    return;
                }
                if (!IsSimpleTarget(invocation.Target))
                {
                    return;
                }
                var rr = ctx.Resolve(invocation) as CSharpInvocationResolveResult;

                if (rr == null)
                {
                    return;
                }
                var lambdaParameters = parameters.ToList();
                var arguments        = rr.GetArgumentsForCall();

                if (lambdaParameters.Count != arguments.Count)
                {
                    return;
                }
                for (int i = 0; i < arguments.Count; i++)
                {
                    var arg = UnpackImplicitIdentityOrReferenceConversion(arguments[i]) as LocalResolveResult;
                    if (arg == null || arg.Variable.Name != lambdaParameters[i].Name)
                    {
                        return;
                    }
                }
                var returnConv = ctx.GetConversion(invocation);

                if (returnConv.IsExplicit || !(returnConv.IsIdentityConversion || returnConv.IsReferenceConversion))
                {
                    return;
                }
                AddIssue(expression, ctx.TranslateString("Expression can be reduced to delegate"), script => {
                    var validTypes = CreateFieldAction.GetValidTypes(ctx.Resolver, expression).ToList();
                    if (validTypes.Any(t => t.FullName == "System.Func" && t.TypeParameterCount == 1 + parameters.Count) && validTypes.Any(t => t.FullName == "System.Action"))
                    {
                        if (rr != null && rr.Member.ReturnType.Kind != TypeKind.Void)
                        {
                            var builder = ctx.CreateTypeSystemAstBuilder(expression);
                            var type    = builder.ConvertType(new TopLevelTypeName("System", "Func", 1));
                            var args    = type.GetChildrenByRole(Roles.TypeArgument);
                            args.Clear();
                            foreach (var pde in parameters)
                            {
                                args.Add(builder.ConvertType(ctx.Resolve(pde).Type));
                            }
                            args.Add(builder.ConvertType(rr.Member.ReturnType));
                            script.Replace(expression, new CastExpression(type, invocation.Target.Clone()));
                            return;
                        }
                    }
                    script.Replace(expression, invocation.Target.Clone());
                });
            }