tags:

views:

121

answers:

1

I am generating assembly on runtime . Problem is that i cant get rid-of it after i am done with it. I need to destroy this assambly what i have created

        string _Errors = "";
        object[] parms = null;

        CodeDomProvider _CodeCompiler = CodeDomProvider.CreateProvider("CSharp"); //new Microsoft.CSharp.CSharpCodeProvider().CreateCompiler();
        CompilerParameters _CompilerParameters = new CompilerParameters();
        _CompilerParameters.GenerateExecutable = false;
        _CompilerParameters.GenerateInMemory = true;
        _CompilerParameters.ReferencedAssemblies.Add("System.Windows.Forms.dll");
        _CompilerParameters.TreatWarningsAsErrors = false;
        _CompilerParameters.CompilerOptions = "/optimize";
        try
        {
            System.CodeDom.Compiler.CompilerResults _CompilerResults = null;
            _CompilerResults = _CodeCompiler.CompileAssemblyFromSource(_CompilerParameters, _SourceCode);
            if (_CompilerResults.Errors.Count > 0)
            {
                _Errors = "";
                foreach (System.CodeDom.Compiler.CompilerError CompErr in _CompilerResults.Errors)
                {
                    _Errors += "Line number " + CompErr.Line +
                                ", Error Number: " + CompErr.ErrorNumber +
                                ", '" + CompErr.ErrorText + ";\r\n\r\n";
                }
                return false;
            }
            else
            {
                _Errors = null;
            }   _CompilerResults.CompiledAssembly = null;
            _CompilerResults = null;
            _CompilerParameters = null;
            _CodeCompiler.Dispose();
            GC.Collect();}catch{}
+1  A: 

I suppose by "get rid of it" you mean "unload". Unfortunatelly (or not so) once loaded into an AppDomain, assemblies cannot be unloaded. To circumvent this, you can generate (or load, for that matter) assembly in a separate AppDomain and then destroy it as needed.

Anton Gogolev
I tried that but still it leeks some where
Woland
Could you update your post with the new code that loads the assembly into a new AppDomain?
Darin Dimitrov