Sign in to follow this  

[.net] C# CodeCompiler disposal?

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;


        #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; }


        #region Constructors

        public VBCompiler(string fileName)
            _fileName = fileName;


        #region Compilation Methods

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


My problem is that it seems after the first compilation, any subsequent compilations fail with the following error:
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

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this