Quantcast
Channel: SharpDevelop Community
Viewing all articles
Browse latest Browse all 1764

Get from TypeReferenceExpression to TypeDeclaration?

$
0
0

I'm using ICSharpCode.Decompiler.Ast.AstBuilder (which is used by SharpDevelop, NRefactory and ILSpy under the hood; has taken inspiration from there too) from code to build an abstract syntax tree from a .NET Common Intermediate Language assembly.

var assembly = AssemblyDefinition.ReadAssembly(assemblyPath);
var astBuilder = new AstBuilder(new DecompilerContext(assembly.MainModule));
astBuilder.AddAssembly(assembly);
// astBuilder.SyntaxTree ...

The syntax tree contains TypeReferenceExpressions whenever a line like Math.Abs() is there. How can I retrieve the TypeDeclaration of the referenced type when it also exists in the same syntax tree?

The closest I got is TypeReferenceExpression.Type.ToTypeReference().Resolve() but this is very heavy to orchestrate, and I already have (I suppose) all the information in the syntax tree. I also tried manual lookup by using namespaces an identifiers but I guess there should be a better way.

I cross-posted this from StackOverflow but figured it's better here.


Viewing all articles
Browse latest Browse all 1764

Trending Articles