0

I already found a way how to change the contents of a function at runtime: https://stackoverflow.com/a/36415711/14727115

But is it possible just to add content to, lets say, a standard constructor? What I originally thought was I create a byte-buffer (malloc/marshall) of size: the size of the method/constructor I want to change + the size of the method to add, and then replace the memory address the original constructor/method refers to, to the new created one.

But that didnt work out well, because I am not sure whether .PrepareMethod handles ConstructorInfo as it does MethodInfo

NHSH
  • 69
  • 7
  • 2
    What is your actual use case? Runtime code replacement would be very uncommon in well designed, legal applications. – JonasH Jun 09 '22 at 12:12
  • @JonasH The problem may sound stupid, but what I try to achieve is loading certain things in the background whenever a class that implements a certain interface is instantiated (similar to abstract class where the parent class'es constructor is called too, whenever a deriving class is instantiated). But interfaces dont have constructors (for obvious reason), so what I thought was, injecting code to every assembly's constructor, that is marked with that particular interface. _ – NHSH Jun 09 '22 at 18:13
  • To me that sounds more like a task for something like a dependency injection container framework. Or to create a factory to construct your classes that may run arbitrary code at that time. – JonasH Jun 10 '22 at 06:42

0 Answers0