diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs index f789c83817..2998446b49 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreateFieldAction.cs @@ -44,9 +44,14 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring return invoke != null && invoke.Target == node; } + internal static Expression GetCreatePropertyOrFieldNode(RefactoringContext context) + { + return context.GetNode(n => n is IdentifierExpression || n is MemberReferenceExpression || n is NamedExpression) as Expression; + } + public IEnumerable GetActions(RefactoringContext context) { - var expr = context.GetNode(n => n is IdentifierExpression || n is MemberReferenceExpression) as Expression; + var expr = GetCreatePropertyOrFieldNode(context); if (expr == null) yield break; @@ -174,6 +179,9 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring } if (expr.Parent is ArrayInitializerExpression) { + if (expr is NamedExpression) + return new [] { resolver.Resolve(((NamedExpression)expr).Expression).Type }; + var aex = expr.Parent as ArrayInitializerExpression; if (aex.IsSingleElement) aex = aex.Parent as ArrayInitializerExpression; diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs index c9c750cad0..2c8fa4ec9b 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/CreatePropertyAction.cs @@ -35,7 +35,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring { public IEnumerable GetActions(RefactoringContext context) { - var identifier = context.GetNode(n => n is IdentifierExpression || n is MemberReferenceExpression) as Expression; + var identifier = CreateFieldAction.GetCreatePropertyOrFieldNode (context); if (identifier == null) yield break; if (CreateFieldAction.IsInvocationTarget(identifier)) @@ -118,6 +118,8 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring return ((IdentifierExpression)expr).Identifier; if (expr is MemberReferenceExpression) return ((MemberReferenceExpression)expr).MemberName; + if (expr is NamedExpression) + return ((NamedExpression)expr).Name; return null; } diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs index f785374796..4370c651de 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreateFieldTests.cs @@ -209,5 +209,29 @@ class Foo "}", result); } + [Test()] + public void TestObjectInitializer () + { + // Not 100% correct input code, but should work in that case as well. + Test (@"class TestClass +{ + void TestMethod () + { + new TestClass { + $NonExistantProperty = 5 + }; + } +}", @"class TestClass +{ + int NonExistantProperty; + void TestMethod () + { + new TestClass { + NonExistantProperty = 5 + }; + } +}"); + } + } } \ No newline at end of file diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs index fe5d43d1fa..f1dfb1176d 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/CreatePropertyTests.cs @@ -276,6 +276,35 @@ class Foo AEnum e = AEnum.B$ar; } } +"); + } + + [Test()] + public void TestPropertyFromObjectInitializer () + { + TestCreateProperty ( + @"class TestClass +{ + void TestMethod () + { + new TestClass { + $NonExistantProperty = 5 + }; + } +} +", @"class TestClass +{ + int NonExistantProperty { + get; + set; + } + void TestMethod () + { + new TestClass { + NonExistantProperty = 5 + }; + } +} "); } }