Call to base constructor

153 views
Skip to first unread message

seesharper

unread,
Dec 23, 2011, 5:44:34 AM12/23/11
to mono-cecil
Hi!

I have this case where I need to inject code in a constructor AFTER
the call to the base constructor.

This is modifying an existing type so I do not have control over
calling the base constructor.

Is there any way I can search through the instructions and detect the
call to the base constructor.

L_0000: ldarg.0
L_0001: call instance void [mscorlib]System.Object::.ctor()
-> Inject code here ........

Is there any generic way I can solve this so I can make sure my code
is injected after the call to the base constructor?

Regards

Bernhard Richter

Gábor Kozár

unread,
Dec 23, 2011, 10:54:10 AM12/23/11
to mono-...@googlegroups.com
That should be pretty simple. When you're modifying a constructor method, just look for the first instruction after which the evaluation stack is empty. That should be the constructor delegation call.

(Disclaimer: I'm not way an IL expert, so I'm not 100% sure about this, but I'd try it this way.)

2011/12/23 seesharper <bernhard...@gmail.com>

--
--
mono-cecil

Carlo Kok

unread,
Dec 23, 2011, 3:31:24 PM12/23/11
to mono-...@googlegroups.com

Look for a *call* to a .ctor, there should be only 1. (new is used to
instantiate while call is only used for base/this calls)


--
Carlo Kok
RemObjects Software

Jb Evain

unread,
Dec 23, 2011, 4:09:22 PM12/23/11
to mono-...@googlegroups.com
On Dec 23, 2011, at 9:31 PM, Carlo Kok wrote:
> Look for a *call* to a .ctor, there should be only 1. (new is used to instantiate while call is only used for base/this calls)

Except when creating structs with a non default ctor ;)

Jb

Reply all
Reply to author
Forward
0 new messages