たとえば、goodDay.cs
クラスがあります。C# コードを使用するように名前を変更する必要がありbadDay.cs
、プロジェクトがまだ正しく機能していることを確認する必要があります。
どうすればいいですか?
たとえば、goodDay.cs
クラスがあります。C# コードを使用するように名前を変更する必要がありbadDay.cs
、プロジェクトがまだ正しく機能していることを確認する必要があります。
どうすればいいですか?
たぶん次のようなもの:
string solutionFolder = @"C:\Projects\WpfApplication10\WpfApplication10";
string CSName = "Goodday.cs";
string newCSName = "BadDay.cs";
string projectFile = "WpfApplication10.csproj";
File.Move(System.IO.Path.Combine(solutionFolder, CSName), System.IO.Path.Combine(solutionFolder, newCSName));
File.WriteAllText(System.IO.Path.Combine(solutionFolder, projectFile),File.ReadAllText(System.IO.Path.Combine(solutionFolder, projectFile)).Replace(CSName,newCSName));
リファクタリング ツールを書きたいようですね。これは非常に難しく、C Sharp コンパイラを大量に実装する必要があります。
幸いなことに、Microsoft は最近、コンパイラを公開しました (そして .net で書き直しました)。Roslynプロジェクトは現在 CTP にあり、C# が何をしているかを理解するのに使用でき、コードのリファクタリングに役立ちます (JetBrains のような企業は、独自の C# パーサーをゼロから作成する必要がありました) 。
これは、ブログ投稿から見つけたサンプルです
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Roslyn.Services;
using Roslyn.Scripting.CSharp;
namespace RoslynSample
{
class Program
{
static void Main(string[] args)
{
RefactorSolution(@"C:\Src\MyApp.Full.sln", "ExternalClient", "ExternalCustomer");
Console.ReadKey();
}
private static void RefactorSolution(string solutionPath, string fileNameFilter, string replacement)
{
var builder = new StringBuilder();
var workspace = Workspace.LoadSolution(solutionPath);
var solution = workspace.CurrentSolution;
if (solution != null)
{
foreach (var project in solution.Projects)
{
var documentsToProcess = project.Documents.Where(d => d.DisplayName.Contains(fileNameFilter));
foreach (var document in documentsToProcess)
{
var targetItemSpec = Path.Combine(
Path.GetDirectoryName(document.Id.FileName),
document.DisplayName.Replace(fileNameFilter, replacement));
builder.AppendFormat(@"tf.exe rename ""{0}"" ""{1}""{2}", document.Id.FileName, targetItemSpec, Environment.NewLine);
}
}
}
File.WriteAllText("rename.cmd", builder.ToString());
}
}
}