我正在爲我的大學課程之一開發簡單的C#編輯器,並且需要向編譯器發送一個.cs文件並收集錯誤(如果存在)並在我的應用程序中顯示它們。換句話說,我想將C#編譯器添加到我的編輯器中。調試器有沒有這樣的事情?訪問編譯器的錯誤和警告
4
A
回答
8
如果使用CodeDomProvider
using System.CodeDom;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
// the compilation part
// change the parameters as you see fit
CompilerParameters cp = CreateCompilerParameters();
var options = new System.Collections.Generic.Dictionary<string, string>();
if (/* you want to use the 3.5 compiler*/)
{
options.Add("CompilerVersion", "v3.5");
}
var compiler = new CSharpCodeProvider(options);
CompilerResults cr = compiler.CompileAssemblyFromFile(cp,filename);
if (cr.Errors.HasErrors)
{
foreach (CompilerError err in cr.Errors)
{
// do something with the error/warning
}
}
1
使用System.Diagnostics.Process
類啓動通用過程並從標準輸入/輸出收集I/O。
對於這個特定場景,我建議你看看Microsoft.CSharp.CSharpCodeProvider
類。它會爲你完成任務。
0
您可以在命令中調用C#編譯器,然後捕獲輸出。
Check here for more information on building from the command line。
相關問題
- 1. 編譯器類型警告和錯誤
- 2. AOT編譯中的警告和錯誤
- 3. 警告:與編譯錯誤
- 4. 警告:與編譯錯誤
- 5. 編譯器警告
- 6. Maven的編譯器警告和錯誤在Eclipse
- 7. 警告:編譯錯誤的過程
- 8. SEH處理程序原因編譯器警告和錯誤
- 9. 編譯器警告libpcap的
- 10. 警告:觸發與編譯錯誤
- 11. 觸發警告:創建編譯錯誤
- 12. 警告:函數創建編譯錯誤
- 13. 編譯警告 - ARM交叉編譯器
- 14. NSJSONSerialization編譯器警告
- 15. CLLocationManager編譯器警告
- 16. 編譯器警告消息
- 17. F#編譯器警告
- 18. 編譯器警告在NSManagedObject
- 19. GWT 2.8編譯器警告
- 20. AFHTTPclient編譯器警告
- 21. 編譯器警告::變種
- 22. C編譯器警告
- 23. C#編譯器警告1685
- 24. Tiny C編譯器和病毒警告
- 25. 警告和SunStudio C++編譯器
- 26. Scala警告,IntelliJ和編譯器標記
- 27. PHP7和Apache編譯警告
- 28. 用戶定義的編譯器警告或C#中的錯誤
- 29. Facebook錯誤警告MY.URL.COM無法訪問
- 30. 你可以顯示編譯器警告的錯誤:無?