Parsing C# source code

穿精又带淫゛_ 提交于 2019-12-08 17:07:27

问题


I'm trying to parse a simple.cs source file using the following code:

CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
var compileUnit = provider.Parse(File.OpenText(filename));

This gives me a NotImplementedException:

"This CodeDomProvider does not support this method"

Is it true that .NET does not provide an implemenation for parsing C# code? Or am I just using this class the wrong way?

Edit: The reason for doing this is that I want to toy around with some methods for static code analysis. Compiling or executing the code is not required for my research.


回答1:


Yes, that's true, the CodeDomProvider is for emitting source code, not reading it. Various companies have their own parsers and recently Microsoft started project Roslyn that provides such features.



来源:https://stackoverflow.com/questions/26093271/parsing-c-sharp-source-code

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!