473,406 Members | 2,220 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

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.GetExecutingAssembly();
Type[] types = currentAssembly.GetTypes();
....//other unrelated code removed...
AppDomain appDomain = AppDomain.CurrentDomain;
AssemblyBuilder assbuild =
appDomain.DefineDynamicAssembly(currentAssembly.Ge tName(),
AssemblyBuilderAccess.Run);
//types[j] is the index in types of the type I need to modify.
ModuleBuilder myModuleBuilder =
assbuild.DefineDynamicModule(types[j].Module.Name);

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

MethodBuilder myMethod = myTypeBuilder.DefineMethod(mi.Name,
MethodAttributes.Public, CallingConventions.Standard, null,
myMethodArgs);

ILGenerator methodIL = myMethod.GetILGenerator();
methodIL.EmitWriteLine("Trace method: " + mi.Name);
methodIL.Emit(OpCodes.Nop,
mi.GetMethodBody().GetILAsByteArray().ToString());
types[j] = myTypeBuilder.CreateType();

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?
Oct 1 '08 #1
2 1996
Lord Zoltar <lo*********@gmail.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 - <sk***@pobox.com>
Web site: http://www.pobox.com/~skeet
Blog: http://www.msmvps.com/jon.skeet
C# in Depth: http://csharpindepth.com
Oct 1 '08 #2
On Oct 1, 1:29*am, Jon Skeet [C# MVP] <sk...@pobox.comwrote:
Lord Zoltar <lord.zol...@gmail.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!
Oct 2 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

16
by: Japcuh | last post by:
How do you write self modifying code in Java? Japcuh (Just Another Perl C Unix Hacker) http://www.catb.org/~esr/faq/hacker-howto.htm#what_is ..0. ...0 000
1
by: vincent ngai | last post by:
Hi folks, I have the following scenario Lib X A lib with 1 class and 1 template class with a static var, the ctor of the class initialises the templated class's var. This works if Lib X has...
12
by: pvinodhkumar | last post by:
1) char* p = "Plato"; p = 'r'; // runtime error 2) char c = "Plato"; c = 'i';// ok.Why no runtime here?Why is the contradiction? cout << c << endl;
7
by: Jan Gregor | last post by:
Hello folks I want to apply changes in my source code without stopping jython and JVM. Preferable are modifications directly to instances of classes. My application is a desktop app using swing...
1
by: Richard Dutton | last post by:
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...
0
by: RYoung | last post by:
I have a custom attribute with an "IsPresent" property with get; set accessors. When the attribute is applied to a field of a class, IsPresent will be false by default. At runtime, I'll read in...
4
by: Mike | last post by:
I have a web service being consume by a new client. The WebMethod they are calling is called Process. So in the WSDL I have has 2 elements, Process and ProcessResponse. <s:schema...
0
by: Bhavini | last post by:
Hi all, I have to implement localization in asp.net application using .resx files, but I have to read and modify these .resx files at runtime, I used ResourceReader class to read that file but...
2
by: anujzcool | last post by:
Is there any way of accessing and modifying configuration properties at runtime.
5
by: IUnknown | last post by:
Ok, we are all aware of the situation where modifying the folder structure (adding files, folders, deleting files, etc) will result in ASP.NET triggering a recompilation/restart of the application....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.