Roslyn: get the symbol for a type defined in a third-party library

扶醉桌前 提交于 2019-12-21 17:31:57

问题


using Roslyn/Microsoft.CodeAnalysis, how can I get the ISymbolfor a third-party type, ie, a type defined in a referenced assembly which is not part of the solution? In my particular case, I'm looking for JSON.NET's JObject, but actually the same question would be valid for BCL stuff like StringBuilder etc.

The only idea I've come up so far is to implement a CSharpSyntaxWalker which looks for all method invocations, property accesses and constructor calls, checks whether they are made on the type I'm interested in and if yes, gets the symbol from the respective SyntaxNode. I'm going to implement this now, but it seems awfully cumbersome. I think there must a better way, but my google-fu has not yielded any relevant results.

Maybe about the background: what I'm trying to do is replace the usage of JObject with the usage of another class.


回答1:


If you can get access to a Compilation you can call Compilation.GetTypeByMetadataName() and pass in the fully qualified metadata name for the symbol.

Be careful of nested classes and generics, their metadata names are different than they're normal fully qualified names. For more see: C# : having a "+" in the class name?

Once you have the symbol you can find all usages via SymbolFinder.FindAllReferences()



来源:https://stackoverflow.com/questions/33374777/roslyn-get-the-symbol-for-a-type-defined-in-a-third-party-library

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