Adding a Static Constructor to a Type.

May 6, 2009 at 10:31 AM
Hi,
I want to add a Static Constructor to a mutable Type and a few lines of code to it as its body.
I dint find any direct API to do so, just like MethodDefinition class.
How do I proceed with it??

Any help?

Thanx in advance.
Purva.
Coordinator
May 7, 2009 at 6:09 AM
Edited May 7, 2009 at 6:12 AM
A static constructor is just a method with a special name and prescribed signature. Make a new mutable MethodDefinition, give it the name ".cctor" and set its IsSpecialName property to true. Do the rest like any other method (you need to initialize every property). Use a mutable SourceMethod for its body. See the PeToPe sample for a start on how to set up the SourceToILConverterProvider instance that is the first parameter to the SourceMethodBody. The other parameters can be null.

Remember to add the new method to the Methods property of the containing type. You need not visit the new method. Its IL will get generated when the PE writer visits the modified object model.

May 13, 2009 at 9:26 AM

Hey hi,
Very Sorry for the late reply.
I got it done correctly.
Thanx a lott.
Purva