Reflection

C

Chris Botha

If I have a class named Class1 and it has a function named Function1, then I
want to determine by what other functions Funtion1 is called.

One should be able to do this using Reflection, as Red Gate has an app named
..NET Reflector that examines the assembly (executable or DLL) and shows the
"used by", and this is what I want to do.
 
P

Philipp Brune

Hi Chris,

i'm afraid, you cannot do this directly via reflection, but what you can
do, is

foreach(Assembly a in [your assemblies])
{
foreach ( Type t in a.GetTypes())
{
foreach ( MethodInfo m in t.GetMethods())
{
// walk through the methods code with this neat little project
// http://www.codeproject.com/KB/cs/sdilreader.aspx
// and check, if your function gets called
}
}
}

Perhaps you could also use mono-cecil, http://www.mono-project.com/Cecil
but i have no idea how this works.

Philipp
 
C

Chris Botha

Hi Philipp, thanks for the answer, I am still a bit stumped. I had a look
at the codeproject and I am with you in your example up till where you say
"// and check, if your function gets called".

I think what you mean is that one has to parse/examine the IL code for each
type/method and from the contents of the IL code (if the target type/method
appears in the IL) determine if the function is called - is this correct?

Thanks.


Philipp Brune said:
Hi Chris,

i'm afraid, you cannot do this directly via reflection, but what you can
do, is

foreach(Assembly a in [your assemblies])
{
foreach ( Type t in a.GetTypes())
{
foreach ( MethodInfo m in t.GetMethods())
{
// walk through the methods code with this neat little project
// http://www.codeproject.com/KB/cs/sdilreader.aspx
// and check, if your function gets called
}
}
}

Perhaps you could also use mono-cecil, http://www.mono-project.com/Cecil
but i have no idea how this works.

Philipp

Chris said:
One should be able to do this using Reflection, as Red Gate has an app
named .NET Reflector that examines the assembly (executable or DLL) and
shows the "used by", and this is what I want to do.
 
C

Chris Botha

Nice, thanks, I'll give it a shot.

I could not find a third party tool to give me the info wanted - I need to
build a database of "who calls who" for an existing project - some functions
should not be in the "call path" of some critical functions, and they are,
and it is not feasible to attempt to trace this manually or at run-time -
the project is big.
One example:
A calls B calls C calls X
A calls D calls X
and X should not be in the call path of A.


Patrice said:
Yes you can do so by checking the IL. Depending on what you are trying to
do, the time & budget you have if a low priced third party tool would be
able to do that...

If I remember the profiler also have basic call graph capabilities.

(I believe also to have seen at research.microsoft.com an assembly
intended to ease IL rewriting and it could have interesting things for the
decode part).

--
Patrice


Chris Botha said:
Hi Philipp, thanks for the answer, I am still a bit stumped. I had a
look at the codeproject and I am with you in your example up till where
you say "// and check, if your function gets called".

I think what you mean is that one has to parse/examine the IL code for
each type/method and from the contents of the IL code (if the target
type/method appears in the IL) determine if the function is called - is
this correct?

Thanks.


Philipp Brune said:
Hi Chris,

i'm afraid, you cannot do this directly via reflection, but what you can
do, is

foreach(Assembly a in [your assemblies])
{
foreach ( Type t in a.GetTypes())
{
foreach ( MethodInfo m in t.GetMethods())
{
// walk through the methods code with this neat little project
// http://www.codeproject.com/KB/cs/sdilreader.aspx
// and check, if your function gets called
}
}
}

Perhaps you could also use mono-cecil, http://www.mono-project.com/Cecil
but i have no idea how this works.

Philipp

Chris Botha schrieb:

One should be able to do this using Reflection, as Red Gate has an app
named .NET Reflector that examines the assembly (executable or DLL) and
shows the "used by", and this is what I want to do.
 
P

Philipp Brune

Patrice said:

Yes, indeed. With this you can look for OpCodes.Call* in the parsed IL
method body an check the invoked MethodInfo against your function. I
worked with this a while ago and remember, that it was pretty easy,
but at this time i haven't got any code to give you - Sorry.

But, perhaps http://www.ndepend.com/ could also be an option for you.

Philipp
 
C

Chris Botha

Thanks for the help guys, I got this going now.


Philipp Brune said:
Yes, indeed. With this you can look for OpCodes.Call* in the parsed IL
method body an check the invoked MethodInfo against your function. I
worked with this a while ago and remember, that it was pretty easy,
but at this time i haven't got any code to give you - Sorry.

But, perhaps http://www.ndepend.com/ could also be an option for you.

Philipp
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top