For a mini-project of sorts that I'm doing, I went ahead and made a small script compiler and script loader based on the code that was presented here. So far, everything's been going great, but now I've run into some trouble. Apparently, my scripts, or my program, is, without my knowledge, trying to load a different DLL from the same path (my windows temp directory) everytime my scripts are invoked. Could someone please take a look at my classes and tell me what's going on?
using System;using System.Collections.Generic;using System.Text;using System.IO;using System.CodeDom.Compiler;namespace TheSimsAITest{ /// <summary> /// Contains methods for doing everything that is neccessary to load /// all interactive object available from a base folder. /// </summary> class InteractionObjectLoader { //All scripts available in the base script folder. FileInfo[] Scripts; ScriptCompiler Compiler; CompilerResults[] Results; public InteractionObjectLoader( string BasePath ) { Compiler = new ScriptCompiler(); Results = new CompilerResults[100]; LoadScriptsFromDir( BasePath ); } /// <summary> /// Helper function for extracting a filename from a filename + extension /// parameter string. /// </summary> private string ExtractFilename(string Str) { string TmpStr; string Result = ""; for(int i = 0; i < Str.Length; i++) { TmpStr = Str.Substring(i, 1); if(TmpStr.Equals(".")) return Result; Result = Result + TmpStr; } return ""; } private void LoadScriptsFromDir( string BasePath ) { DirectoryInfo DirInfo = new DirectoryInfo( BasePath ); Scripts = DirInfo.GetFiles( "*.cs" ); int Counter = 0; foreach( FileInfo TempScript in Scripts ) { Counter++; Results[Counter] = Compiler.CompileScript( DirInfo.FullName + "\\" + TempScript.Name ); Results[Counter].CompiledAssembly.GetType( ExtractFilename(TempScript.Name) ).GetMethod( "Init" ).Invoke( null, null ); } } }}
using System;using System.Collections.Generic;using System.Text;using Microsoft.CSharp;using System.CodeDom;using System.CodeDom.Compiler;using System.Reflection;namespace TheSimsAITest{ /// <summary> /// Basic compiler for compiling and loading C# /// files in external object files. /// </summary> class ScriptCompiler { CSharpCodeProvider CodeProvider; ICodeCompiler CodeCompiler; //This program's assembly. Assembly ProgramAssembly; public ScriptCompiler() { CodeProvider = new CSharpCodeProvider(); CodeCompiler = CodeProvider.CreateCompiler(); //Get this program's assembly, so we may aquire it's path //during compilation. ProgramAssembly = Assembly.GetEntryAssembly(); } public CompilerResults CompileScript(string ScriptName) { CompilerParameters Params = new CompilerParameters(); Params.GenerateExecutable = false; Params.GenerateInMemory = true; Params.IncludeDebugInformation = false; Params.TreatWarningsAsErrors = false; Params.ReferencedAssemblies.Add( "System.dll" ); //For messagebox debugging etc. Params.ReferencedAssemblies.Add( "System.Windows.Forms.dll" ); //Enables object scripts to use all classes in this program. Params.ReferencedAssemblies.Add( ProgramAssembly.Location ); CompilerResults results = CodeCompiler.CompileAssemblyFromFile( Params, ScriptName ); return results; } }}
The exception is thrown on this line:
Results[Counter].CompiledAssembly.GetType( ExtractFilename(TempScript.Name) ).GetMethod( "Init" ).Invoke( null, null );
The exception is a FileNotFoundException, and the error looks like this:
"Could not load file or assembly 'file///C:\Users\Afr0\AppData\Local\Temp\73ihye8c.dll', or one of it's dependencies. The system cannot find the file specified."
What's weird is that the name of the dll changes everytime the program is run, but the path stays constant. Any help is greatly appreciated! Thanks!