Delete any occurrence of Property within body of a method.

SyntaxEditor .NET Languages Add-on for Windows Forms Forum

Posted 11 years ago by BorodaAlex
Version: 4.0.0243
Avatar
I have the class on C#. E.g it contains the property 'A':

private string a_;
public string A
{
    get { return a_; }
    set { a_ = value; }
}
This class also contains the method 'B' that uses Property 'A' in the various cases within body.

public void B()
{
 if (!string.IsNullOrEmpty(A))
    A = string.Format("{0}", A.ToLower());
    // and others cases.
}
I need to delete any occurrences of 'A' within body of method 'B'.
It sounds like this might be done via enumerating all statements of MethodDeclaration of 'B' and if it contains simple name equal 'A' delete it like this:

CompilationUnit cu = (CompilationUnit)document_.SemanticParseData;            
            foreach (TypeDeclaration typeDeclaration in cu.Types)
            {                
                foreach (AstNodeBase childNode in typeDeclaration.ChildNodes)
                {
                    MethodDeclaration node = childNode as MethodDeclaration;
                     if (node != null)
                     {
                        if (node.Name.Text == "B")
                        {
                            //  1. go over  node.ChildNodes and get statements
                            //  2. Statement st = node as IfStatement;  // there could  be switch to check all variants of statements.
                            //  3 . Get SimpleName and compare to 'A'
                            //  if step 3 return true replace like this :
                            // document_.ReplaceText(DocumentModificationType.Replace, simpleName.TextRange, string.Empty);
                        }
                     }
                }                               
            }

It appears this is very difficult because there are a lot of types of Statements and they could be used in manifold cases.

What means could be used to resolve this task?

Thanks in advance.

Comments (1)

Posted 11 years ago by Actipro Software Support - Cleveland, OH, USA
Avatar
Maybe create a visitor based on our ActiproSoftware.SyntaxEditor.Addons.DotNet.AstVisitor (it uses the visitor pattern) and go through your tree that way. With that it's nice since you only override methods for the type of nodes you care about.

To run it call Accept on the root node to visit.


Actipro Software Support

The latest build of this product (v2018.1 build 0342) was released 12 days ago, which was after the last post in this thread.

Add Comment

Please log in to a validated account to post comments.