why does reflection.emit work with dynamic assemblies only.it is possible to load an executable and fiddle with the pe format and alter the il of functions. but it is not possible to rewrite the il of a function in the currently executing assembly using reflection emit services. why is that feature not supported.<br/> <br/> thanks in advance<br/>© 2009 Microsoft Corporation. All rights reserved.Thu, 25 Jun 2009 16:36:14 Z626d7a85-5627-41d0-9718-ed8155209f43http://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#626d7a85-5627-41d0-9718-ed8155209f43http://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#626d7a85-5627-41d0-9718-ed8155209f43iamrohitbangahttp://social.msdn.microsoft.com/Profile/en-US/?user=iamrohitbangawhy does reflection.emit work with dynamic assemblies only.it is possible to load an executable and fiddle with the pe format and alter the il of functions. but it is not possible to rewrite the il of a function in the currently executing assembly using reflection emit services. why is that feature not supported.<br/> <br/> thanks in advance<br/>Sun, 21 Jun 2009 09:22:25 Z2009-06-21T09:23:42Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#b8338312-203e-4cca-8c04-550b36c14dechttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#b8338312-203e-4cca-8c04-550b36c14decKarel Zikmundhttp://social.msdn.microsoft.com/Profile/en-US/?user=Karel%20Zikmundwhy does reflection.emit work with dynamic assemblies only.<p>My guess is that there is/was not major customer scenario for such feature. Do you have one? Can you describe it? Maybe there is alternate solution which we can help you with (e.g. using delegates).<br/><br/>Implementing something like this would be quite difficult in current CLR design and the benefit is questionable. One scenario where something similar is needed are profilers - they need to instrument IL. There are special profiler APIs to support that scenario, but emitting of the IL has to happen via native API, not via Reflection.<br/><br/>-Karel</p>Mon, 22 Jun 2009 16:22:51 Z2009-06-22T16:22:51Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#9f67df4e-3e6d-4d23-a01f-55239b7ffa8ahttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#9f67df4e-3e6d-4d23-a01f-55239b7ffa8ap.b.ahttp://social.msdn.microsoft.com/Profile/en-US/?user=p.b.awhy does reflection.emit work with dynamic assemblies only.One benefit would be to offer better support for AOP and runtime interception in general. You can off course do it with proxies but that has its limitations - like intercepted methods have to be virtual. You can also do it with a post compiler or by using the profiling API w. IL rewriting but those have limitations as well : you cannot instrument signed assemblies for which you don't have the key, profiliing API disables strong name verification and it has performance implications which make its use questionable for production systems.   <br/> <br/> <br/> <br/> <br/>Mon, 22 Jun 2009 17:38:48 Z2009-06-22T17:38:48Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#8929eaf7-f98c-43cc-8c14-c727d84e0b29http://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#8929eaf7-f98c-43cc-8c14-c727d84e0b29David Bromanhttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20Bromanwhy does reflection.emit work with dynamic assemblies only.Hi, everyone.  As p.b.a. mentioned, a post-compiler step is an option, and some profiler tools do that.  And yes, it will blow away any strong name verification unless you can resign it afterward.<br/><br/>Using the profiling API at run-time is another option.  Note that this dose not turn off strong name verification--however, the strong name / signature verification happens when the assembly is loaded (or, I believe when it was added to the GAC if it's in the GAC).  The profiler then has the power after that verification is complete to modify the IL in-memory without fear of encountering strong name signing issues.  In fact, there are several production monitoring tools that use the profiling API to rewrite IL in memory.  You can find a quickie overview of the procedure here: <a href="http://blogs.msdn.com/davbr/archive/2007/03/06/creating-an-il-rewriting-profiler.aspx">http://blogs.msdn.com/davbr/archive/2007/03/06/creating-an-il-rewriting-profiler.aspx</a><br/><br/>The limitations with the profiling API are around the kinds of metadata changes you can make, and around the &quot;window of opportunity&quot;--you have one opportunity to modify metadata and IL, which is before they get used.  Once classes are loaded / IL is JITted, your opportunity is over.  Since it is a common request to be able to modify metadata &amp; IL later on, we are looking into what it would take to offer that as a feature in a future release of the CLR.  Another limitation, as mentioned above, is that the profiling API can only be used by native code.  You cannot use managed code to do your instrumentation.<br/><br/>It does sound nice for us to allow the Reflection.Emit methods to work against arbitrary assemblies.  But as Karel said, this would be a huge undertaking for the CLR team, and with the alternatives mentioned above, it's not very high on our priority list.  Still, we'd be interested in hearing more about your scenario that cannot be done with currently available techniques.<br/><br/><br/>Thanks,<br/>DaveWed, 24 Jun 2009 16:47:13 Z2009-06-24T16:47:13Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#8c4ec882-d002-4b3b-accb-d5d4847c0661http://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#8c4ec882-d002-4b3b-accb-d5d4847c0661p.b.ahttp://social.msdn.microsoft.com/Profile/en-US/?user=p.b.awhy does reflection.emit work with dynamic assemblies only.Dave,<br/> <br/> I was referring to code access permissions - are those enabled as well when profiling is on ?<br/> <br/> PaulWed, 24 Jun 2009 17:19:28 Z2009-06-24T17:19:28Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#8c1a67b7-8cc7-4cbe-83d6-a8da98756b6bhttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#8c1a67b7-8cc7-4cbe-83d6-a8da98756b6bDavid Bromanhttp://social.msdn.microsoft.com/Profile/en-US/?user=David%20Bromanwhy does reflection.emit work with dynamic assemblies only.Hi, Paul.<br/><br/>Yes, code access security is enforced while profiling is on.<br/><br/><br/>DaveWed, 24 Jun 2009 18:22:55 Z2009-06-24T18:22:55Zhttp://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#24d4abef-15c8-4a30-89be-f48fc333b3c9http://social.msdn.microsoft.com/Forums/en-US/netfxtoolsdev/thread/626d7a85-5627-41d0-9718-ed8155209f43#24d4abef-15c8-4a30-89be-f48fc333b3c9p.b.ahttp://social.msdn.microsoft.com/Profile/en-US/?user=p.b.awhy does reflection.emit work with dynamic assemblies only.Thanks for clarifying that, I was under the wrong impression that having a profiler active disables some security checks.<br/> <br/> Paul<br/>Wed, 24 Jun 2009 19:22:35 Z2009-06-24T19:22:35Z