From ITypeMemberDefinition, can I get the information defined in the method?

SyntaxEditor .NET Languages Add-on for WPF Forum

Posted 11 months ago by Seito Shirata
Version: 21.1.3


I want to implement the cross-reference functionality by using C# Syntax Edtior.
For that, I want to get the information defined in each method.

For example in the code below, from 'TestMethod',
I want to get the information of 'localVar' and analyze that its type is 'TestClass'.

public void TestMethod(){
     var localVar = new TestClass();

From ProjectAssembly.SrouceFiles I get the ITypeMemberDefinition.
But, ITypeMemberDefinition, which kind is Method , does not has the member providing the information in the method.

How can I get it? Or is there any other way to access the definition in the method?


Comments (2)

Posted 11 months ago by Actipro Software Support - Cleveland, OH, USA


If you run our parser on the C# text (which is what happens when the C# code is in a document with our add-on's C# syntax language attached), part of the parse data result is an abstract syntax tree (AST) that has information on the entire document, including the insides of methods.  For reflection purposes, we take that AST and simplify it to the public APIs we find.  ITypeMemberDefinition is part of the relection-level end of things, which is why it doesn't retain anything about the method contents.  Reflection data is used to primarily drive what shows in IntelliPrompt.  AST information from the current document being edited is also merged into IntelliPrompt by our resolver, which is how variables in the method being edited show up.

Back to your question, reflection data (i.e. ITypeMemberDefinition) won't contain anything about the insides of methods since that would take up a lot of extra memory to track and it's not needed for our purposes.  You can walk the AST of a parsed C# document to look for references to a certain type.  This will likely be a fairly involved process though because you'd have to handle possible inline or using-declared namespaces.  Our C# resolver in the add-on can handle a lot of this if you have the code loaded in a document with the CSharpSyntaxLanguage defined, and you could possibly use it to resolve specific identifiers.  Overall though, you may find it easier and more performant to try and get this kind of information from a separate C# compiler like Roslyn.

This documentation topic tells you how to quickly and synchronously parse a string containing C# code into parse data with an AST.

Actipro Software Support

Posted 11 months ago by Seito Shirata

Thank you.

By using AST, I can access to the information that I wanted.
I will try by using AST.

The latest build of this product (v22.1.2) was released 3 months ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.