Get all used types

175 views
Skip to first unread message

Alex B

unread,
Jun 17, 2011, 1:59:58 PM6/17/11
to mono-cecil
Hellow,

Is there a way to get all types used inside C# method ie:

public int foo(string str)
{
Bar bar = new Bar();
string x = "test";
TEST t = bar.GetTEST();
}

would return: Bar, string and TEST.

Jb Evain

unread,
Jun 17, 2011, 2:57:03 PM6/17/11
to mono-...@googlegroups.com
Hi,

On Fri, Jun 17, 2011 at 7:59 PM, Alex B <alex...@gmail.com> wrote:
> Is there a way to get all types used inside C# method ie:
>
> public int foo(string str)
> {
>  Bar bar = new Bar();
>  string x = "test";
>  TEST t = bar.GetTEST();
> }
>

There's no builtin way. See for instance:

http://groups.google.com/group/mono-cecil/browse_thread/thread/dfb05686e35cbcad/981d606149de1e99?lnk=gst&q=used#981d606149de1e99

Jb

Alex B

unread,
Jun 18, 2011, 9:40:02 AM6/18/11
to mono-cecil
Hellow, JB.

Try your advice (http://groups.google.com/group/mono-cecil/
browse_thread/thread/dfb05686e35cbcad/981d606149de1e99?
lnk=gst&q=used#981d606149de1e99)

>Just iterate over the instructions in the body of MyMethod. Switch on
>the opcode of the instruction, if its OperandType is InlineType, the
>operand of the Instruction will be a TypeReference.

This is test application:

namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Class1 cl = new Class1();
Console.WriteLine(cl.Test());
}
}
}

Now I try to get all types in Main method:

var module = ModuleDefinition.ReadModule(fileName);
var type = module.Types.First(tt => tt.FullName ==
"ConsoleApplication1.Program");
var method = type.Methods.First(mm => mm.FullName == "System.Void
ConsoleApplication1.Program::Main(System.String[])");

foreach (var instruction in method.Body.Instructions)
{
if (instruction.OpCode.OperandType == OperandType.InlineType)
{
Console.WriteLine(instruction);
}
}

There are no instruction.OpCode.OperandType == OperandType.InlineType
What I do wrong?
The main task is to get all types used in assembly.


On 17 июн, 22:57, Jb Evain <jbev...@gmail.com> wrote:
> Hi,
>
> On Fri, Jun 17, 2011 at 7:59 PM, Alex B <alexey...@gmail.com> wrote:
> > Is there a way to get all types used inside C# method ie:
>
> > public int foo(string str)
> > {
> >  Bar bar = new Bar();
> >  string x = "test";
> >  TEST t = bar.GetTEST();
> > }
>
> There's no builtin way. See for instance:
>
> http://groups.google.com/group/mono-cecil/browse_thread/thread/dfb056...
>
> Jb

Gábor Kozár

unread,
Jun 18, 2011, 10:35:06 AM6/18/11
to mono-...@googlegroups.com
You don't have any direct type references in your test application.
If you check the IL, you'll notice that it looks something like this:

newobj void Class1::.ctor(void)
call string Class1::Test(void)
call System.Console.WriteLine(string)

All the operands are method references.
However, consider the following:

ldnull
castclass Class1
pop

Here the operand of the 'castclass' instruction is a TypeReference.

Hope it's clear now.

2011/6/18 Alex B <alex...@gmail.com>
--
--
mono-cecil

Reply all
Reply to author
Forward
0 new messages