Modifying class at runtime

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Lord Zoltar

    #1

    Modifying class at runtime

    Is it possible with the classes in Reflection.Emit to modify a method
    of an existing class?
    I would like to print some diagnostic messages every time certain
    methods are called. I don't want to start adding code to every part of
    my program that calls these methods, and the code came as a DLL so I
    can't modify the source.

    Here's something close to what I've tried:

    Assembly currentAssembly = Assembly.GetExe cutingAssembly( );
    Type[] types = currentAssembly .GetTypes();
    ....//other unrelated code removed...
    AppDomain appDomain = AppDomain.Curre ntDomain;
    AssemblyBuilder assbuild =
    appDomain.Defin eDynamicAssembl y(currentAssemb ly.GetName(),
    AssemblyBuilder Access.Run);
    //types[j] is the index in types of the type I need to modify.
    ModuleBuilder myModuleBuilder =
    assbuild.Define DynamicModule(t ypes[j].Module.Name);

    TypeBuilder myTypeBuilder = myModuleBuilder .DefineType(t.N ame,
    TypeAttributes. Public);

    MethodBuilder myMethod = myTypeBuilder.D efineMethod(mi. Name,
    MethodAttribute s.Public, CallingConventi ons.Standard, null,
    myMethodArgs);

    ILGenerator methodIL = myMethod.GetILG enerator();
    methodIL.EmitWr iteLine("Trace method: " + mi.Name);
    methodIL.Emit(O pCodes.Nop,
    mi.GetMethodBod y().GetILAsByte Array().ToStrin g());
    types[j] = myTypeBuilder.C reateType();

    I had hoped that the line
    types[j]=myTypeBuilder. CreateType();
    but when I try to use objects of this type, the logging statement does
    not appear. Is what I'm trying to do even possible?
  • Jon Skeet [C# MVP]

    #2
    Re: Modifying class at runtime

    Lord Zoltar <lord.zoltar@gm ail.comwrote:
    Is it possible with the classes in Reflection.Emit to modify a method
    of an existing class?
    I would like to print some diagnostic messages every time certain
    methods are called. I don't want to start adding code to every part of
    my program that calls these methods, and the code came as a DLL so I
    can't modify the source.
    If you can't modify the source, you may *legally* not be allowed to do
    anything. However, PostSharp (http://www.postsharp.org) may be able to
    help you.

    --
    Jon Skeet - <skeet@pobox.co m>
    Web site: http://www.pobox.com/~skeet
    Blog: http://www.msmvps.com/jon.skeet
    C# in Depth: http://csharpindepth.com

    Comment

    • Lord Zoltar

      #3
      Re: Modifying class at runtime

      On Oct 1, 1:29 am, Jon Skeet [C# MVP] <sk...@pobox.co mwrote:
      Lord Zoltar <lord.zol...@gm ail.comwrote:
      Is it possible with the classes in Reflection.Emit to modify a method
      of an existing class?
      I would like to print some diagnostic messages every time certain
      methods are called. I don't want to start adding code to every part of
      my program that calls these methods, and the code came as a DLL so I
      can't modify the source.
      >
      If you can't modify the source, you may *legally* not be allowed to do
      anything. However, PostSharp (http://www.postsharp.org) may be able to
      help you.
      >
      Heh it's not so much a legal matter as I compiled an old project to
      DLL and didn't really want to add debug statements all over it and
      rebuild it... I thought it would be much cleaner to just add the debug
      logging in the new project, and I was also kinda curious to play with
      the Emit namespace...
      That PostSharp system looks very interesting, I'll definitely have to
      give it a look. Thanks!

      Comment

      Working...