Re: Modifying a type at runtime

Tech-Archive recommends: Speed Up your PC by fixing your registry

From: Nicholas Paldino [.NET/C# MVP] (mvp_at_spam.guard.caspershouse.com)
Date: 04/22/04


Date: Thu, 22 Apr 2004 08:46:00 -0400

Richard,

    It is not possible to inject code into an assembly that is already
loaded. You will have to create a new, dynamic assembly in another app
domain (as you already know).

    There is no way to say "base the new assembly on this one", from what I
can tell about defining a dynamic assembly.

    Check out the article in MSDN magazine by Dharma Shukla, Simon Fell, and
Chris Sells titled "Aspect-Oriented Programming Enables Better Code
Encapsulation and Reuse" located at (watch for line wrap):

http://msdn.microsoft.com/msdnmag/issues/02/03/AOP/

    Also, you might be able to simulate aspect oriented programming by using
some of the profiling interfaces for the CLR (which have hooks for when IL
is compiled into native code), but it is a messy affair.

    Honestly, I think it might be much, much easier to do what you want
using attributes and defining some limitations on your components which
would help set up context for the attributes (for example, you must use a
class factory so that you can set a context of some kind on creation, etc,
etc)

    Hope this helps.

-- 
               - Nicholas Paldino [.NET/C# MVP]
               - mvp@spam.guard.caspershouse.com
"Richard Dutton" <nottudhcir@no.spam.hotmail.com> wrote in message
news:UGOhc.56$WK1.53@newsfe3-win.server.ntli.net...
> I would like to be able to modify a type at runtime so that I can add an
IL
> prologue and epilogue to its methods (either to implement some kind of
> aspect-oriented programming framework, or a simple profiling tool). I know
> that a simlpe way to achieve the same behaviour, at least for a type's
> virtual methods, is to derive a new type and add my code before and after
> calls to the inner object's methods, but for my own edification I'd like
to
> try to actually inject the code into the existing type.
>
> TypeBuilder, MethodRental and their cohorts in Reflection.Emit look like
> they are capable of performing such injection, but only on types in a
> dynamic assembly so my questions are these:
> Is it at all possible to use Reflection.Emit.*Builder on types in an
> assembly that has been loaded from disc (and acquired using
> Assembly.GetExecutingAssembly, for example).
> If not, is it possible somehow to copy an Assembly into an AssemblyBuilder
> (or Module into ModuleBuilder and so on) in order to modify that.
> Is there another dynamic way to perform the injection (i.e. not using
> il(d)asm).
>
> Many thanks,
>
> Richard Dutton
>
>


Relevant Pages

  • Re: Damn Im Good!
    ... Why all the weaseling on defining a word you keep using? ... guilty (in the context of breaking a law) ... Not only do I lie about what others are claiming, ...
    (comp.sys.mac.advocacy)
  • Re: [BUG] x86_64 pci_map_sg modifies sg list - fails multiple map/unmaps
    ... and you are defining dma_length to just use the ... If you want to modify the length field to zero terminate ... send the line "unsubscribe linux-kernel" in ...
    (Linux-Kernel)
  • Re: Tomcat default context for virtual host
    ... If you specify a context path of an empty string, ... defining the default web application for this Host, ... This tells me that if I define a Context and set the path to an empty ... I should be able to enter just the virtual host name as a URL ...
    (comp.lang.java.programmer)
  • Integrating an ODE function with extra parameters (t,y,p)->(t,y)
    ... dy = zeros; % a column vector ... Now I want to modify the function rigid, ... parameter (I want to avoid defining extra global variables and pass ...
    (comp.soft-sys.matlab)
  • Tomcat default context for virtual host
    ... According to the documentation for the Tomcat 6 <Context> element, ... If you specify a context path of an empty string, ... defining the default web application for this Host, ...
    (comp.lang.java.programmer)