mercredi 17 janvier 2018

C# reflection and finding all references of a Class/Property in my project solution

I have a class named Test and I instantiated that class in several other classes. Is there any way I could find all references to the Class "Fix" programmatically. I know I could find that with Find All References in VisualStudio but I want to achieve this programmatically for my requirement. Any help, please.

Example: Fix.cs

namespace Testing
{
    public class Fix
    {
        public string fixName = "Name";
    }
}

CodeFix.cs

namespace Testing
{
    public class CodeFix
    {
        private string GetName()
        {
            Fix fix = new Fix();
            return fix.fixName;
        }
    }
}

Now I want to write code to Find All References to a variable "fixName" in my solution (namespace Testing). Is there any way I could achieve this programmatically either for a class or for a variable. I would like to get the result with the fileName and variable referenced code line.

Example:

Result
{
FileName="CodeFix.cs",
Line=return fix.fixName;
}

I am supposed to automate the below scenario for which I need Find All References. I should not use public fields, so When I find a public field like "fixName" I should change the fixName from public to private and then expose the same using a property.

private string fixName;
public string FixName
{
    get { return fixName; }
    set { fixName = value; }
}

so I should change the file codeFix.cs - line return fix.fixName;must be changed with the property name return fix.FixName;.

I have a VSIX project to fix code violation, I need to implement this "automated fix" to avoid using public fields. By any chance Is there a way to get the Visual Studio -> Find All References data?





Aucun commentaire:

Enregistrer un commentaire