Take the 2-minute tour ×

How can I define my own constructor for the System.Object type, such that when any byref type gets initialized, it calls my custom constructor? Is there a way to achieve this?

Edit: After I read part of http://msdn.microsoft.com/en-us/magazine/cc163791.aspx, there might be a way to do it. Is it possible to just replace method pointer somewhere in method table?

share|improve this question
No, you can not monkey-patch .NET. Why are you trying to? –  Marc Gravell Aug 1 '12 at 22:18
Love "monkey-patch". lol –  Simon Whitehead Aug 1 '12 at 22:32
@Simon it is the term for the scenario: en.wikipedia.org/wiki/Monkey_patch –  Marc Gravell Aug 1 '12 at 22:44
@Marc Oh I know, I just love the term lol. –  Simon Whitehead Aug 1 '12 at 22:46
Not sure why this is getting close votes. It may be difficult to find because of how specific it is, but the subject matter is sure to be the question of others somewhat new to the language. –  Marc Aug 2 '12 at 15:07
add comment

1 Answer

up vote 1 down vote accepted

It is not possible to override a class's (or every class)'s constructor(s). You can inherit from a particular class if it isn't sealed, and add additional logic to that class's constructor. You could also use a factory method. Another option you could look at would be to use Castle, or some other proxy library.

share|improve this answer
add comment

Your Answer


