[Mono-devel-list] Can one "hook" API functions?

Jonathan Pryor jonpryor at vt.edu
Sat Jan 31 01:07:50 EST 2004


On Wed, 2004-01-28 at 06:54, Martin C.Atkins wrote:
<snip/>
> Is there any way in Mono, and hopefully a standard way that will
> also work on MS .NET, to "hook" system functions/classes?
> 
> To be more specific, I want to replace the File and Directory
> Open/Close/Read/Write classes and methods with my own ones that
> augment the behaviour of the standard routines, and are transparently
> available to all programs, old and new, that previously used the
> standard implementations.
<snip/>

No.

There are only two basic mechanisms to change the behavior of an
existing function: virtual functions, and delegates (or something using
delegates, such as an event).

Both of these require prior planning on behalf of the original class
author, as functions are non-virtual by default, and delegates/events
would require that the programmer actually do something to explicitly
support it.

Furthermore, even if the functions were virtual, you'd still need to
make sure that, somehow, the user was creating instances of your class,
not your base class.  There are design patterns to do this, but for
anything created directly by the user (Strings, arrays, files, pretty
much everything) there is no way to substitute your class for the
original class.

Why is this the case?  There are likely several reasons, but two that I
can think of are performance and security.  By ensuring that people can
only override functions in explicit ways, the runtime can better
optimize those patterns, helping to improve performance.  And if anyone
could replace any function, you'd have security holes introduced in no
time.

 - Jon




More information about the Mono-devel-list mailing list