Before reading this blog post, I hope you can read this article by Allan.
Http://www.cnblogs.com/zlgcool/archive/2008/10/12/1309616.html
When I visited the garden, I occasionally saw Allan. This article provides two dynamic compilation solutions. The first method may cause memory problems due to inconvenient uninstallation. The second method will save the dll to the bin directory, load the dll, call the method in the dll, and delete the dll. This series of operations will inevitably affect efficiency and waste of resources. I was wondering if I could not simplify this series of operations. So I have this blog post. Thank you first.Feng Ge used his time to solve the problems for the comrades in the garden. This practice is worth learning.
Let's start with the text:
The Assembly cannot be detached independently, but the program domain can be detached. The second method of Allan. can be used for reference.
First look at the solution
AutoCompiled:Provides the function of dynamically compiling source code.
AppDomainTest:Caller
Form1.cs:Create AppDomain and load autoCompiled
RemoteLoader. cs:Provides remote access calls
REFERENCE The autoCompiled project in Winform.
The general idea is to get all the compilation process (Compiled. cs class) of the code to the AppDomain for execution, and RemoteLoader. cs provides remote access.
Winform code:
Buttondomainclick (strSourceCode = obj = CreateAppDomainExec (strSourceCode, CreateAppDomainExec (strSourceCode, typeName, === AppDomain. createDomain (, = (RemoteLoader) appDomain. createInstanceAndUnwrap (Assembly. getExecutingAssembly (). getName (). fullName, (RemoteLoader ). fullName); remoteLoader. loadAssembly (); result = remoteLoader. compiledExec (strSourceCode, typeName, FunctionName); =View Code
RemoteLoader. cs class:
LoadAssembly (= LoadFromAssembly (= CompiledExec (SourceCode, typeName, objClass = assembly. CreateInstance (); strResult = objClass. GetType (). InvokeMember (, BindingFlags. InvokeMethod, objClass,View Code
Compiled. cs class:
GetCompiledByString (strSourceCode, typeName, ==== objClass = objAssembly. createInstance (typeName); (objClass =) objResult = objClass. getType (). invokeMember (FunctionName, BindingFlags. invokeMethod, objClass, [] {View Code
Procedure:
1: After clicking the button, the code will create a new application domain AppDomain
2: load the assembly in the new ApppDomain program domain [remoteLoader. LoadAssembly ("autoCompiled ");]
3: Get the class to be called in the assembly [object objClass = assembly. CreateInstance ("autoCompiled. Compiled ");]
4: Call the class method for compilation and return the execution result [GetCompiledByString]
5: uninstall AppDomain
If you call AppDomain. Unload again, an error is reported. It indicates that the Assembly has indeed been uninstalled along with AppDomain.
This method can indeed avoid the cumbersome steps of Allan. method 2, but it is essentially done by loading the compiled references. The call method is reflected twice each time.
First time: Call the referenced method
Second: Call the method in the automatically compiled code
I want to add the result Assembly after dynamic compilation to the AppDomain, rather than adding the code compilation process to the AppDomain. However, due to the limited level, I hope you can give me some advice.