Sie können dies mit Roslyn API tun.Sie können eine kleine C# -Anwendung erstellen, fügen Sie Roslyn API, öffnen Sie Ihre WinForm und asp.net-Projekt mit Roslyn und tun etwas wie Sie wollen.
Zuerst Klasse erstellen, die Ihre Dateien erhalten und neue Datei zurückgeben.
public class FieldToPropertyConverter: CSharpSyntaxRewriter
{
FieldToPropertyConverter(string YourSolutionFilePath)
{
OpenSolution(YourSolutionFilePath);
}
public void OpenSolution(string path)
{
// OPEN your solution here
}
public Solution s ;
public void Convent()
{
foreach (var p in s.Projects)
foreach (var d in p.Documents)
{
var cus = Visit(d.GetSyntaxRootAsync().Result);
// save cus to solution
}
// save you set solution to your work space
}
public override SyntaxNode VisitFieldDeclarationSyntax(FieldDeclarationSyntax f)
{
PropertyDeclaractionSyntax p =SyntaxFactory.PropertyDeclaraction(f.Declaraction.Type, f.Declaraction.Variables.First().Identifier);
return p;
}
}
Sie diese Klasse verwenden können, wie diese
FieldToPropertyConverter c = new FieldToPropertyConverter("solutionFilePath");
c.Convent();
c.Save();
Sind Sie 'public Property mit {get; einstellen; } Syntax? Das dauert nicht lange. – ManoDestra
Warum erstellen Sie die Felder überhaupt, verwenden Sie einfach Auto-Eigenschaften statt, tun Sie 'öffentliche bool Foo {bekommen; einstellen; } ' –
Gibt es einen bestimmten Grund, warum Sie das tun? Mein Projekt VSDiagnostics (https://github.com/VSDiagnostics/VSDiagnostics) hat einen Code-Fix, der nicht-statische/statische öffentliche/interne Felder in Eigenschaften ändert. Analyzer-Implementierung: https://github.com/VSDiagnostics/VSDiagnostics/blob/develop/VSDiagnostics/VSDiagnostics/VSDiagnostics/Diagnostics/General/NonEncapsulatedOrMutableField/NonEncapsulatedOrMutableFieldAnalyzer.cs –