diff --git a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs index 3f40e01725..291b507f75 100644 --- a/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs +++ b/ICSharpCode.NRefactory.CSharp/Refactoring/CodeActions/GeneratePropertyAction.cs @@ -1,4 +1,4 @@ -// +// // GenerateProperty.cs // // Author: @@ -51,7 +51,7 @@ namespace ICSharpCode.NRefactory.CSharp.Refactoring } } var field = initializer.Parent as FieldDeclaration; - if (field == null) { + if (field == null || field.HasModifier(Modifiers.Readonly) || field.HasModifier(Modifiers.Const)) { yield break; } var resolveResult = context.Resolve(initializer) as MemberResolveResult; diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs index a094c3c002..409542ff94 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/ContextActionTestBase.cs @@ -84,7 +84,11 @@ namespace ICSharpCode.NRefactory.CSharp.CodeActions protected static void TestWrongContext (string input) where T : ICodeActionProvider, new () { - ICodeActionProvider action = new T (); + TestWrongContext (new T(), input); + } + + protected static void TestWrongContext (ICodeActionProvider action, string input) + { var context = TestRefactoringContext.Create (input); bool isValid = action.GetActions (context).Any (); if (!isValid) diff --git a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/GeneratePropertyTests.cs b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/GeneratePropertyTests.cs index 98de6781c2..509fac1bd9 100644 --- a/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/GeneratePropertyTests.cs +++ b/ICSharpCode.NRefactory.Tests/CSharp/CodeActions/GeneratePropertyTests.cs @@ -1,4 +1,4 @@ -// +// // GeneratePropertyTests.cs // // Author: @@ -107,5 +107,31 @@ class TestClass int myField, myOtherField; }"); } + + [Test] + public void CannotGeneratePropertyForReadOnlyField() + { + TestWrongContext ( + new GeneratePropertyAction (), + "using System;" + Environment.NewLine + + "class TestClass" + Environment.NewLine + + "{" + Environment.NewLine + + " readonly int $myField;" + Environment.NewLine + + "}" + ); + } + + [Test] + public void CannotGeneratePropertyForConst() + { + TestWrongContext ( + new GeneratePropertyAction (), + "using System;" + Environment.NewLine + + "class TestClass" + Environment.NewLine + + "{" + Environment.NewLine + + " const int $myField = 0;" + Environment.NewLine + + "}" + ); + } } } \ No newline at end of file