0

I am working on a small Project in which I need to find all the references to other types in a C# class. For example :

    class C1 : C2
    {
          C3 var1;
          C4 var2;

        public C5 SomeMethod(C6 p1, C7 p2)
        {
             C8 varC8 = new C8(p1,p2);
             varC8.DoSomething();
        }
    }

As in the above example I have a class which has references to other classes C2, C3,C4,C5,C6,C7,C8. I want to know what are all the types that are being used by this Class C1 and any method/property calls that are used in this class.

I have tried using Mono.Cecil library and I can get till the point of finding out the types of local variables, properties, fields, return types of methods and parameters of the methods. I can not get to the point of going through each line of code and finding out how to find out any declarations or method calls are part of that line.

Can some one help me with this analysis?

Thanks, Satish

Satish
  • 445
  • 2
  • 4
  • 15
  • It means you want find out all the types this class rely? not only the fields,properties , also including the argument every method used? – Ju-Hsien Lai Jan 20 '16 at 06:47
  • If you use Resharper, it will 'gray' out properties, classes etc that are not used. That's the closest that I know... *JetBrain* (authors of *Reharper*) also sell something called *dotCover* that might get you there – Jens Meinecke Jan 20 '16 at 06:51
  • try using reflection to enumerate all members and put their types in a hashset – slawekwin Jan 20 '16 at 06:54
  • 1
    Do you need this at runtime or are you looking for something like a visual studio plugin? – C.Evenhuis Jan 20 '16 at 07:03
  • @C.Evenhuis: Right now I am looking for this information at run time. But at the end I want to build a VS plugin for this. – Satish Jan 20 '16 at 09:07
  • Here i posted a full working example to find property/method references at runtime with reflection, its easy expandable to find all references: http://stackoverflow.com/a/33034906/4035472 – thehennyy Jan 20 '16 at 09:43
  • Mono.Cecil should also work. What have you tried so far, can you post some code? With Mono.Cecil you sould be able to loop all instructions and then you can inspect the instructions operand and thats the reference you are searching for. – thehennyy Jan 20 '16 at 10:07

0 Answers0