Jump to content
  • Advertisement
Sign in to follow this  
Jonny_S

[.net] C# CodeCompiler disposal?

This topic is 3206 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, I have a simple code compiler class that I have to run a couple of times, the code for the class is shown below:
using System;
using System.Collections.Generic;
using System.Text;
using System.CodeDom.Compiler;
using Microsoft.CSharp;
using System.CodeDom;
using System.Reflection;
using System.Xml;
using System.IO;

namespace ILTools.DynamicCompiler
{
    public class VBCompiler:IDynamicCompiler
    {
        #region Private Members

        private CodeDomProvider _codeCompiler;
        private CompilerResults _compilerResults;
        private string _fileName;

        #endregion

        #region Derived Properties

        public CompilerErrorCollection Errors
        {
            get { return _compilerResults.Errors; }
        }

        public bool HasErrors
        {
            get { return _compilerResults.Errors.HasErrors; }
        }

        public Assembly GeneratedAssembly
        {
            get { return _compilerResults.CompiledAssembly; }
        }

        #endregion

        #region Constructors

        public VBCompiler(string fileName)
        {
            _fileName = fileName;
            Compile();
        }

        #endregion

        #region Compilation Methods

        private void Compile()
        {
            _codeCompiler = CodeDomProvider.CreateProvider("VB");
            CompilerParameters compilerParams = new CompilerParameters();
            compilerParams.ReferencedAssemblies.Add("system.dll");
            compilerParams.OutputAssembly = string.Format("{0}.exe", _fileName);
            compilerParams.GenerateExecutable = true;
            compilerParams.IncludeDebugInformation = true;
            compilerParams.CompilerOptions = "/optimize- /debug:full";
            _compilerResults = _codeCompiler.CompileAssemblyFromFile(compilerParams, _fileName);
        }

        #endregion

    }
}
My problem is that it seems after the first compilation, any subsequent compilations fail with the following error:
Quote:
Unable to write to output file 'test.exe': System Error &H80070020&
I've been able to track it down to this section of code and I can only assume it's to do with the fact that the assembly is still stored in memory. Is there any way to 'release' the generated assembly so I can recompile? Thanks Jonathan [edit] What tags do you use to get nice syntax highlighted code?

Share this post


Link to post
Share on other sites
Advertisement
AFAIK, the only way to release a loaded assembly is to load it into a separate AppDomain at the beginning, and then unload the AppDomain.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!