I used CSharpCodeProvider to compile and generate a new namespace in the memory for temporary use only. But this namespace should be deleted from the memory after certain period of time in order to allow for the next generated code to override the same identifiers of all generated classes and methods.
Asked
Active
Viewed 732 times
2
-
2Have a look at [AppDomains](http://msdn.microsoft.com/en-us/library/system.appdomain.aspx) you can unload them... – rene Dec 27 '12 at 09:41
3 Answers
1
No way. Class unload only happens - attention - when unloading a AppDomain.
Your best bet is to do all the code generation in a child-appdomain (will be same process), but that will not be trivial (the child appdomain needs proxies of all reachable object via remoting).

TomTom
- 61,059
- 10
- 88
- 148
-
Is there any other way to use generated code temporary and then release it from the memory ? (But not appdomains ...) How difficult it would be to use child appdomain ? And how exactly i will f.ex call method from there ? childappdomain.namespace.type.method() ? i am not sure if i will success to deal with this anyway . – David von Tamar Dec 27 '12 at 10:00
-
@DavidDiamond - In general, it means you will end up putting 90% of your code in the child AppDomain, to avoid most serialization/remoting issues – Simon Mourier Dec 27 '12 at 10:10
-
@Simon That may not be true - it seriously depends on what the application does. But it is not a light undertaking for a Junior developer - one has to know some internals and how to lay out a well designed application. But it is still quite simple compared to some of the inner arcanes of extending for example WCF or working with Direct3d ;) – TomTom Dec 27 '12 at 10:43
-
Quite simple compared to D3D ? No . WCF is also not that hard . My main development right now is 3D game engine (with physics HLSL and what ever could be), but also i wanted to develop some script language for my project to allow somehow Real-Time programming . – David von Tamar Dec 27 '12 at 11:41
-
WDF is teribl once you try to do things like your own channels - the innjards are complexy and not pretty documented. With AppDomains at least the "needed knowledge" is low - at the end it is a lot of work designing the objects you Need to expose and making them, but it is simple and easy to understand. – TomTom Dec 27 '12 at 12:30
1
There is only one way a loaded type can be unloaded: if it is a part of a collectible assembly.
But collectible assemblies can be only created through Reflection.Emit, not CSharpCodeProvider. Because of that, I'm not sure if using a collectible assembly is an option for you. If not, you will need some other option (like unloading AppDomains).

svick
- 236,525
- 50
- 385
- 514
-
I think , i Should try this with "collectible assemblies" . Thanks . – David von Tamar Dec 28 '12 at 05:06
0
- Load generated assemblies into child AppDomain
- To call default AppDomain, for cross-domain commutation use marshaled by reference proxies
- Or use WCF with Named Pipe binding
- Unload child AppDomain

abatishchev
- 98,240
- 88
- 296
- 433
-
But if i will release Appdomain , then all it's instances will be also lost . It will be same as application restart before updating code (bad for me) . I think i will try the "collectible assembly" solution . if it will not help then i will try the ICorDebug that will solve my problem finally . (in VS you can change the compiled source objects during runtime in debug season . it is called code Injection , and this what i am trying to do just with another ways because ICorDebug is really complicated and huge API) – David von Tamar Dec 28 '12 at 05:18
-
This is why you Isolate every script block into a SEAPARATE appdomain. And using WCF is slow her - cross appdomain removing is heavily optimized. – TomTom Dec 28 '12 at 08:32
-