我正在爲我的大學課程之一開發簡單的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. Maven的編譯器警告和錯誤在Eclipse
- 6. 編譯器警告libpcap的
- 7. 警告:觸發與編譯錯誤
- 8. 警告:函數創建編譯錯誤
- 9. C編譯器警告
- 10. GWT 2.8編譯器警告
- 11. 編譯器警告消息
- 12. C#編譯器警告1685
- 13. 警告和SunStudio C++編譯器
- 14. Tiny C編譯器和病毒警告
- 15. Flex flex任務中的flex編譯器錯誤(警告)消息
- 16. 你可以顯示編譯器警告的錯誤:無?
- 17. iOS和libxml的編譯器警告的問題
- 18. 在pom.xml中配置Java編譯器 - >錯誤/警告
- 19. C++ const:編譯器怎麼沒有給出警告/錯誤
- 20. 爲庫函數定義編譯器警告/錯誤
- 21. Eclipse編譯器錯誤/警告首選項
- 22. 當沒有錯誤時,CSharpCodeProvider不會返回編譯器警告
- 23. TFS構建 - 將編譯器警告視爲錯誤?
- 24. 編譯錯誤安裝python-igraph:警告視爲錯誤
- 25. iPhone編譯警告
- 26. 在編譯器輸出中查找警告和錯誤的正則表達式
- 27. VS2010中的Supress編譯器警告
- 28. 的boost :: ASIO async_read_some async_read_until編譯器警告
- 29. 的Xcode 3.2.3編譯器警告
- 30. Java編譯器警告的嚴重性