我正在研究在使用VisualStudioWorkspace更新現有代碼的Visual Studio擴展(VSIX)中使用Roslyn編譯器。花了幾天的時間閱讀這篇文章,似乎有幾種方法可以實現這一目標......我只是不確定哪種方法適合我。Roslyn將新方法添加到現有類
好了,讓我們假設用戶有他們的解決方案在Visual Studio 2015年開放,他們點擊我的推廣和(通過一種形式),他們告訴我,他們希望下面的方法定義添加到一個接口:
GetSomeDataResponse GetSomeData(GetSomeDataRequest request);
他們還告訴我接口的名稱,它是ITheInterface。
界面中已經有一些代碼:
namespace TheProjectName.Interfaces
{
using System;
public interface ITheInterface
{
/// <summary>
/// A lonely method.
/// </summary>
LonelyMethodResponse LonelyMethod(LonelyMethodRequest request);
}
}
好了,這樣我就可以使用加載接口文檔如下:
Document myInterface = this.Workspace.CurrentSolution?.Projects?
.FirstOrDefault(p
=> p.Name.Equals("TheProjectName"))
?.Documents?
.FirstOrDefault(d
=> d.Name.Equals("ITheInterface.cs"));
那麼,什麼是現在添加的最佳方式我的新方法到這個現有的接口,最好是在XML評論(三斜槓評論)寫?請記住請求和響應類型(GetSomeDataRequest和GetSomeDataResponse)可能實際上還不存在。我對此很新,所以如果你可以提供代碼示例,那就太棒了。
UPDATE
我決定(可能)最好的辦法是簡單地在一些文字注入,而不是試圖以編程方式建立的方法聲明。
我嘗試以下,但結束了一個例外,我不理解:
SourceText sourceText = await myInterface.GetTextAsync();
string text = sourceText.ToString();
var sb = new StringBuilder();
// I want to all the text up to and including the last
// method, but without the closing "}" for the interface and the namespace
sb.Append(text.Substring(0, text.LastIndexOf("}", text.LastIndexOf("}") - 1)));
// Now add my method and close the interface and namespace.
sb.AppendLine("GetSomeDataResponse GetSomeData(GetSomeDataRequest request);");
sb.AppendLine("}");
sb.AppendLine("}");
檢查這一點,這一切都很好(我真正的代碼添加格式和XML註釋,但去除,爲了清楚)。
因此,知道這些是不可變的,我試圖挽救它,如下所示:
var updatedSourceText = SourceText.From(sb.ToString());
var newInterfaceDocument = myInterface.WithText(updatedSourceText);
var newProject = newInterfaceDocument.Project;
var newSolution = newProject.Solution;
this.Workspace.TryApplyChanges(newSolution);
但是,這創造了以下異常:
bufferAdapter is not a VsTextDocData
在Microsoft.VisualStudio.Editor。 Implementation.VsEditorAdaptersFactoryService.GetAdapter(IVsTextBuffer bufferAdapter) at Microsoft.VisualStudio.Editor.Implementation.VsEditorAdaptersFactoryService.GetDocumentBuffer(IVsTextBuffer bufferAdapter) 在Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.InvisibleEditor..ctor(的IServiceProvider的ServiceProvider,字符串文件路徑,布爾needsSave,布爾needsUndoDisabled) 在Microsoft.VisualStudio.LanguageServices.RoslynVisualStudioWorkspace.OpenInvisibleEditor(IVisualStudioHostDocument hostDocument) 在Microsoft.VisualStudio。 LanguageServices.Implementation.ProjectSystem.DocumentProvider.StandardTextDocument.UpdateText(SourceText newText) 在Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.VisualStudioWorkspaceImpl.ApplyDocumentTextChanged(documentId documentId,SourceText newText) 在Microsoft.CodeAnalysis.Workspace。ApplyProjectChanges(ProjectChanges projectChanges) 在Microsoft.CodeAnalysis.Workspace.TryApplyChanges(解決方案newSolution) 在Microsoft.VisualStudio.LanguageServices.Implementation.ProjectSystem.VisualStudioWorkspaceImpl.TryApplyChanges(解決方案newSolution)
您可能需要通過調用'SourceText.WithChanges(new TextChange(...))'來更改現有的'SourceText'(它附加了附加的源文件信息),[請參閱本答案](http: //stackoverflow.com/a/37553697/155005)爲例。 – m0sa