Hi,
You'd need to do some control flow analysis and figure out what kinds
of values could be in local variables and on the stack as the this
reference when the method is called. For the most simple cases, you
can simply ask what type a ParameterDefinition, FieldReference,
VariableDefinition etc is, but for anything complex like conditionals,
you'd have to do deeper analysis, e.g.:
class Program
{
class A
{
public virtual void Do() {}
}
class B : A
{
public override void Do() {}
}
static void Main(string[] args)
{
var obj = args == null ? new A() : new B();
obj.Do();
}
}
Will turn into:
.locals init (class Program/A V_0)
IL_0000: nop
IL_0001: ldarg.0
IL_0002: brfalse.s IL_000b
IL_0004: newobj instance void Program/B::.ctor()
IL_0009: br.s IL_0010
IL_000b: newobj instance void Program/A::.ctor()
IL_0010: stloc.0
IL_0011: ldloc.0
IL_0012: callvirt instance void Program/A::Do()
IL_0017: nop
IL_0018: ret
Here, you'd have to analyze the possible code paths to IL_0012 and
figure out what would be on the stack in each path.
(Hope I explained this somewhat decently; I'm sleep-deprived!)
Regards,
Alex
On Thu, Jul 28, 2011 at 8:25 PM, deedee <[email protected]> wrote:
> I need to know the type of an object, that calls a method.
> For example.
>
> public void MethodName1()
> {
> XClass obj1 = abc;
> // some code here
>
> obj1.Method2(true);
>
> }
>
> Method2(bool) is in a different class, which is being inherited by
> multiple classes. So I need to find the type of object that calls this
> method.
>
> I want the output to be "XClass" when I need to find the type of the
> object that calls Method2(bool). Is there a way I can do that? Right
> now the instructions just shows ldloc.x (where x is some index no.). I
> am aware that operands can be cast as MethodReference and
> FieldReferences and many other types depending on the case, but what
> shall I do here?
>
> Thanks!
>
> --
> --
> mono-cecil
--
--
mono-cecil