public static Type CompileAndGetType(string typename, string language, string key, string file, ArrayList assemblies) { CompilerResults result = CachingCompiler.Compile(language, key, file, assemblies); if (result.NativeCompilerReturnValue != 0) { using (StreamReader reader = new StreamReader(file)) { throw new CompilationException(file, result.Errors, reader.ReadToEnd()); } } Assembly assembly = result.CompiledAssembly; if (assembly == null) { using (StreamReader reader = new StreamReader(file)) { throw new CompilationException(file, result.Errors, reader.ReadToEnd()); } } Type type = assembly.GetType(typename, true); InsertType(type, file); return(type); }
public virtual Type GetCompiledType() { Type type = CachingCompiler.GetTypeFromCache(parser.InputFile); if (type != null) { return(type); } ConstructType(); string lang = parser.Language; string tempdir; string compilerOptions; int warningLevel; Provider = CreateProvider(parser.Context, lang, out compilerOptions, out warningLevel, out tempdir); if (Provider == null) { throw new HttpException("Configuration error. Language not supported: " + lang, 500); } CompilerParameters parameters = CompilerParameters; parameters.IncludeDebugInformation = parser.Debug; parameters.CompilerOptions = compilerOptions + " " + parser.CompilerOptions; parameters.WarningLevel = warningLevel; bool keepFiles = (Environment.GetEnvironmentVariable("MONO_ASPNET_NODELETE") != null); if (tempdir == null || tempdir == "") { tempdir = DynamicDir(); } TempFileCollection tempcoll = new TempFileCollection(tempdir, keepFiles); parameters.TempFiles = tempcoll; string dllfilename = Path.GetFileName(tempcoll.AddExtension("dll", true)); parameters.OutputAssembly = Path.Combine(DynamicDir(), dllfilename); CompilerResults results = CachingCompiler.Compile(this); CheckCompilerErrors(results); Assembly assembly = results.CompiledAssembly; if (assembly == null) { if (!File.Exists(parameters.OutputAssembly)) { results.TempFiles.Delete(); throw new CompilationException(parser.InputFile, results.Errors, "No assembly returned after compilation!?"); } assembly = Assembly.LoadFrom(parameters.OutputAssembly); } results.TempFiles.Delete(); Type mainClassType = assembly.GetType(MainClassType, true); if (parser.IsPartial) { // With the partial classes, we need to make sure we // don't have any methods that should have not been // created (because they are accessible from the base // types). We cannot do this normally because the // codebehind file is actually a partial class and we // have no way of identifying the partial class' base // type until now. if (!isRebuilding && CheckPartialBaseType(mainClassType)) { isRebuilding = true; parser.RootBuilder.ResetState(); return(GetCompiledType()); } } return(mainClassType); }
public override Type GetCompiledType() { Type type = CachingCompiler.GetTypeFromCache(parser.PhysicalPath); if (type != null) { return(type); } if (parser.Program.Trim() == "") { type = Type.GetType(parser.ClassName, false); if (type == null) { type = parser.GetTypeFromBin(parser.ClassName); } CachingCompiler.InsertTypeFileDep(type, parser.PhysicalPath); return(type); } string lang = parser.Language; string compilerOptions; string tempdir; int warningLevel; CodeDomProvider provider; Provider = provider = CreateProvider(parser.Context, lang, out compilerOptions, out warningLevel, out tempdir); if (Provider == null) { throw new HttpException("Configuration error. Language not supported: " + lang, 500); } CompilerParameters compilerParameters; CompilerParameters = compilerParameters = CachingCompiler.GetOptions(parser.Assemblies); compilerParameters.IncludeDebugInformation = parser.Debug; compilerParameters.CompilerOptions = compilerOptions; compilerParameters.WarningLevel = warningLevel; bool keepFiles = (Environment.GetEnvironmentVariable("MONO_ASPNET_NODELETE") != null); TempFileCollection tempcoll; tempcoll = new TempFileCollection(tempdir, keepFiles); compilerParameters.TempFiles = tempcoll; inputFile = tempcoll.AddExtension(provider.FileExtension); Stream st = File.OpenWrite(inputFile); StreamWriter sw = new StreamWriter(st); sw.WriteLine(parser.Program); sw.Close(); string dllfilename = Path.GetFileName(tempcoll.AddExtension("dll", true)); compilerParameters.OutputAssembly = Path.Combine(DynamicDir(), dllfilename); CompilerResults results = CachingCompiler.Compile(this); CheckCompilerErrors(results); Assembly assembly = results.CompiledAssembly; if (assembly == null) { if (!File.Exists(compilerParameters.OutputAssembly)) { throw new CompilationException(inputFile, results.Errors, "No assembly returned after compilation!?"); } assembly = Assembly.LoadFrom(compilerParameters.OutputAssembly); } results.TempFiles.Delete(); type = assembly.GetType(parser.ClassName, true); CachingCompiler.InsertTypeFileDep(type, parser.PhysicalPath); return(type); }