Search code examples
asp.netbuildprovider

BuildProvider: Is it possible to specify the name of the assembly to be generated?


I created a custom virtual path provider and build provider and was wondering if it was possible to somehow specify the name of the assembly that get's generated, rather then letting asp.net come up with a name?


Solution

  • To answer my own question, it is possible with little gymnastics. One can get reference to CodeDomProvider from AssemblyBuilder and from there on it is up to you what you want to do. Something like this

     public override void GenerateCode(AssemblyBuilder assemblyBuilder)
            {
                string outputName = VirtualPath.Substring(VirtualPath.LastIndexOf('/') + 1);
                outputName = outputName.Substring(0, outputName.LastIndexOf('.'));
    
                _compilationContext = (CompilationContext)HttpContext.Current.Items[outputName];
    
               // var tw = assemblyBuilder.CreateCodeFile(this);
               // tw.Write(_compilationContext.Content);
               // tw.Flush();
               // tw.Close();
    
                //TODO: inject this 
                var factory = new DirectCompilerServiceFactory();
                var compilerService = factory.CreateCompilerService(assemblyBuilder.CodeDomProvider);
    
                compilerService.CompileAssembly(_compilationContext);
    
            }