2
// ConvertLambdaToDelegateAction.cs
5
// Simon Lindgren <simon.n.lindgren@gmail.com>
7
// Copyright (c) 2012 Simon Lindgren
9
// Permission is hereby granted, free of charge, to any person obtaining a copy
10
// of this software and associated documentation files (the "Software"), to deal
11
// in the Software without restriction, including without limitation the rights
12
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
13
// copies of the Software, and to permit persons to whom the Software is
14
// furnished to do so, subject to the following conditions:
16
// The above copyright notice and this permission notice shall be included in
17
// all copies or substantial portions of the Software.
19
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
20
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
21
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
22
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
23
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
24
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
27
using ICSharpCode.NRefactory.CSharp.Resolver;
28
using System.Collections.Generic;
29
using ICSharpCode.NRefactory.TypeSystem;
31
namespace ICSharpCode.NRefactory.CSharp.Refactoring
33
[ContextAction("Convert lambda to anonymous delegate",
34
Description = "Converts a lambda to an anonymous delegate.")]
35
public class ConvertLambdaToAnonymousDelegateAction : SpecializedCodeAction<LambdaExpression>
37
#region implemented abstract members of SpecializedCodeAction
38
protected override CodeAction GetAction(RefactoringContext context, LambdaExpression node)
40
if (context.Location < node.StartLocation || context.Location >= node.Body.StartLocation)
43
var lambdaResolveResult = context.Resolve(node) as LambdaResolveResult;
44
if (lambdaResolveResult == null)
47
return new CodeAction(context.TranslateString("Convert to anonymous delegate"), script => {
48
BlockStatement newBody;
49
if (node.Body is BlockStatement) {
50
newBody = (BlockStatement)node.Body.Clone();
52
var expression = (Expression)node.Body.Clone();
55
if (RequireReturnStatement(context, node)) {
56
statement = new ReturnStatement(expression);
59
statement = new ExpressionStatement(expression);
62
newBody = new BlockStatement {
68
var method = new AnonymousMethodExpression (newBody, GetParameters(lambdaResolveResult.Parameters, context));
69
script.Replace(node, method);
74
IEnumerable<ParameterDeclaration> GetParameters(IList<IParameter> parameters, RefactoringContext context)
76
if (parameters == null || parameters.Count == 0)
78
var result = new List<ParameterDeclaration> ();
79
foreach (var parameter in parameters) {
80
var type = context.CreateShortType(parameter.Type);
81
var name = parameter.Name;
82
ParameterModifier modifier = ParameterModifier.None;
84
modifier |= ParameterModifier.Ref;
86
modifier |= ParameterModifier.Out;
87
result.Add (new ParameterDeclaration(type, name, modifier));
92
static bool RequireReturnStatement (RefactoringContext context, LambdaExpression lambda)
94
var type = LambdaHelper.GetLambdaReturnType (context, lambda);
95
return type != null && type.ReflectionName != "System.Void";