提交 aac94f49 编写于 作者: S Sam Harwell 提交者: GitHub

Merge pull request #18399 from MaximRouiller/master

Fix "Hide base member" code fix for constants
......@@ -34,7 +34,7 @@ class App : Application
class App : Application
{
public static new App Current { get; set; }
public new static App Current { get; set; }
}");
}
......@@ -64,7 +64,7 @@ public static void Method()
class App : Application
{
public static new void Method()
public new static void Method()
{
}
}");
......@@ -93,5 +93,43 @@ class App : Application
public new int Test;
}");
}
[WorkItem(18391, "https://github.com/dotnet/roslyn/issues/18391")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddNew)]
public async Task TestAddNewToConstant()
{
await TestInRegularAndScriptAsync(
@"class Application
{
public const int Test = 1;
}
class App : Application
{
[|public const int Test = Application.Test + 1;|]
}",
@"class Application
{
public const int Test = 1;
}
class App : Application
{
public new const int Test = Application.Test + 1;
}");
}
[WorkItem(14455, "https://github.com/dotnet/roslyn/issues/14455")]
[Fact, Trait(Traits.Feature, Traits.Features.CodeActionsAddNew)]
public async Task TestAddNewToConstantInternalFields()
{
await TestInRegularAndScriptAsync(
@"class A { internal const int i = 0; }
class B : A { [|internal const int i = 1;|] }
",
@"class A { internal const int i = 0; }
class B : A { internal new const int i = 1; }
");
}
}
}
\ No newline at end of file
......@@ -4,6 +4,8 @@
using System.Threading;
using Microsoft.CodeAnalysis.Formatting;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using Microsoft.CodeAnalysis.Editing;
using Microsoft.CodeAnalysis.Shared.Extensions;
namespace Microsoft.CodeAnalysis.CSharp.CodeFixes.HideBase
{
......@@ -34,30 +36,8 @@ protected override async Task<Document> GetChangedDocumentAsync(CancellationToke
private SyntaxNode GetNewNode(Document document, SyntaxNode node, CancellationToken cancellationToken)
{
SyntaxNode newNode = null;
var propertyStatement = node as PropertyDeclarationSyntax;
if (propertyStatement != null)
{
newNode = propertyStatement.AddModifiers(SyntaxFactory.Token(SyntaxKind.NewKeyword)) as SyntaxNode;
}
var methodStatement = node as MethodDeclarationSyntax;
if (methodStatement != null)
{
newNode = methodStatement.AddModifiers(SyntaxFactory.Token(SyntaxKind.NewKeyword));
}
var fieldDeclaration = node as FieldDeclarationSyntax;
if (fieldDeclaration != null)
{
newNode = fieldDeclaration.AddModifiers(SyntaxFactory.Token(SyntaxKind.NewKeyword));
}
//Make sure we preserve any trivia from the original node
newNode = newNode.WithTriviaFrom(node);
return newNode.WithAdditionalAnnotations(Formatter.Annotation);
var generator = SyntaxGenerator.GetGenerator(_document);
return generator.WithModifiers(node, generator.GetModifiers(node).WithIsNew(true));
}
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册