How can I use CodeDOM to create and load an assembly in an AppDomain?

妖精的绣舞 提交于 2019-12-04 09:12:24

Chicken and egg problem. You need a little bootstrapper assembly that you can load in the new AppDomain. With a well-known class that loads the CodeDom generated assembly and gets it going.

Doing this with GenerateInMemory is pretty pointless, you would have to serialize that into the new AppDomain. That's just a bunch of overhead, might as well load it from disk, it is there anyway. And it is already in memory. The file system cache's memory. Loading it will be very fast since it doesn't actually have to be read off the disk.

I found the answer that I was looking for at http://www.softwareinteractions.com/blog/2010/2/7/loading-and-unloading-net-assemblies.html. He has a nice article detailing the creation of an AppDomain and loading an assembly as a plugin. I followed his example and was able to create an AppDomain, create a proxy for my ExpressionEvaluator class factory and successfully call it and receive results.

Just use AssemblyBuilderAccess.Run when you define the dynamic assembly http://msdn.microsoft.com/en-us/library/system.reflection.emit.assemblybuilderaccess.aspx

The dynamic assembly can be executed, but not saved.

Ray

Where does the CompilCode method come from in all of this? Seems to be that is the most important part. And you decide leave it out?

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!