Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
Hi All,

I am interested to know how to do the following with Reflection.Emit in C#.

I have an assembly in my program already loaded and from it I can read all the classes and methods inside of it. However, I would like to add more methods in this current Assembly. I understand the procedure for creating new assemblies, classes and methods, but what about having an assembly and classes already existing but with new methods.

I will apreciate any advice.

Thanks in advance!

Peter
Posted
Comments
Sergey Alexandrovich Kryukov 11-Apr-12 18:15pm    
Interesting question, my 5.
--SA
Sergey Alexandrovich Kryukov 11-Apr-12 18:25pm    
I am not sure if adding declarations into already loaded assembly is possible at all; to best of my knowledge, it is not possible. So, maybe what I recommend is somewhat alternative way of doing things, but it certainly worth considering and trying out.
--SA

1 solution

One approach is using such an interesting thing as System.Reflection.Emit.DynamicMethod; please see:
http://msdn.microsoft.com/en-us/library/system.reflection.emit.dynamicmethod.aspx[^].

The idea is: you don't have to create methods which are counted as being declared in certain type. In fact, on low level, even the instance (non-static) method declared in some class or a structure has only two aspects binding it to a certain type: 1) it has full access to all its members, 2) it has the first implicit parameter used to pass "this", the reference to the class instance. You can implement both features with dynamic methods, and also such an exotic thing as (1) without (2) — this is done via the flag skipVisibility passed in some constructors (http://msdn.microsoft.com/en-us/library/xc6e708b.aspx[^]), and passing "this" is implemented as adding the first parameter explicitly and using the parameter Owner.

This is described here:
http://msdn.microsoft.com/en-us/library/exczf7b9.aspx[^].

Pay attention for the section "To define and execute a dynamic method that is bound to an object".

The dynamic method can be called directly using its Invoke methods or by creation a delegate using CreateDelegate methods. The fastest way of calling the dynamic method repetitively is calling this delegate. One problem is that if you want to call the delegate with different targets, you should re-bind it by calling CreateDelegate again and again. This is very expensive, as well as repetitive call to the Invoke. One resolution of this problem is using dynamic method without binding to the object, but passing a reference explicitly, which also supports polymorphism. For access, you need to use skipVisibility with the constructor referenced above.

Now, the amazing thing is that Reflection does not count dynamic methods, at least not as methods belonging to any types! They exist only during run time, so it might not be exactly a solution you are looking for. However, for any practical purposes, they work exactly as any "regular" methods called through delegate instances, with decent performance of direct method calls. Besides, in certain situations you can create highly effective manually emitted code with extraordinary flexibility typical for Reflection but without its performance cost! I would highly recommend to try it out. If you already learned how to emit the code (which is the hardest part), using dynamic methods may seem relatively easy for you.

—SA
 
Share this answer
 
v6
Comments
Espen Harlinn 11-Apr-12 18:21pm    
Excellent reply :-D
Sergey Alexandrovich Kryukov 11-Apr-12 18:30pm    
Thank you, Espen. I'm doing interesting things with Reflection.Emit right now, for my work. This is the amazing field of programming.
--SA
petBorromeo 11-Apr-12 18:47pm    
Hi SA, Thanks for the complete and professional answer. I will try out what you just commented me. At the moment I am trying a simple idea that came to my mind. I will let you know the results for sure. Thanks!
Sergey Alexandrovich Kryukov 11-Apr-12 20:17pm    
You are very welcome.
I would be interested to know what approach you are trying to apply and discuss it.
Best wishes,
--SA
El_Codero 11-Apr-12 19:27pm    
Very good thanks my 5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900