private void OnAssemblyFailedLoading(AssemblyFailedLoadingEventArgs e) { if (this.AssemblyFailedLoading != null) { this.AssemblyFailedLoading(this, e); } }
private void LoadSourceFile(string filename, SourceFileLanguage language) { bool loaded = false; string errorMsg = ""; // Try compiling the script first CompilerResults res = CompileScript(filename, sourceFileReferencedAssemblies, this.GetCodeDomLanguage(language)); // Check for compilation errors if (res.Errors.Count <= 0) { // No errors, then loop through the types in the assembly // We don't stop after the first time we find our interface, this way the // Assembly or source file could have multiple types with the desired interface // Instead of a 1 interface per file relationship foreach (Type t in res.CompiledAssembly.GetTypes()) { // Try getting the clientinterface from the type if (t.BaseType == typeof(ClientInterface)) { try { // Load an instance of this particular Extension and add it to our extensions list Extension <ClientInterface> newExt = new Extension <ClientInterface>(filename, ExtensionType.SourceFile, (ClientInterface)res.CompiledAssembly.CreateInstance(t.FullName, true)); newExt.InstanceAssembly = res.CompiledAssembly; extensions.Add(newExt); OnAssemblyLoaded(new AssemblyLoadedEventArgs(filename)); loaded = true; } catch (Exception ex) { // Some problem in actually creating an instance errorMsg = "Error Creating Instance of Compiled Source File (" + filename + "): " + ex.Message; } } } // We got through without loading an instance, so we didn't find types with the expected interface if (!loaded && String.IsNullOrEmpty(errorMsg)) { errorMsg = "Expected interface (" + typeof(ClientInterface).ToString() + ") was not found in any types in the compiled Source File"; } } else { // Compile time errors errorMsg = "Source File Compilation Errors were Detected"; } if (!loaded) { // Instance was never created, so let's report it and why AssemblyFailedLoadingEventArgs e = new AssemblyFailedLoadingEventArgs(filename); e.ExtensionType = ExtensionType.SourceFile; e.SourceFileCompilerErrors = res.Errors; e.ErrorMessage = errorMsg; this.OnAssemblyFailedLoading(e); } }
private void LoadCompiledFile(string filename) { bool loaded = false; string errorMsg = ""; Assembly compiledAssembly = null; // Load the assembly to memory so we don't lock up the file byte[] assemblyFileData = System.IO.File.ReadAllBytes(filename); // Load the assembly try { compiledAssembly = Assembly.Load(assemblyFileData); } catch { errorMsg = "Compiled Assembly (" + filename + ") is not a valid Assembly File to be Loaded."; } if (compiledAssembly != null) { // Go through the types we need to find our clientinterface foreach (Type t in compiledAssembly.GetTypes()) { // Try getting the interface from the current type if (t.BaseType == typeof(ClientInterface)) { try { // Load an instance of this particular Extension and add it to our extensions list Extension <ClientInterface> newExt = new Extension <ClientInterface>(filename, ExtensionType.Compiled, (ClientInterface)compiledAssembly.CreateInstance(t.FullName, true)); newExt.InstanceAssembly = compiledAssembly; extensions.Add(newExt); OnAssemblyLoaded(new AssemblyLoadedEventArgs(filename)); loaded = true; } catch (Exception ex) { // Creating an instance failed for some reason, pass along that exception message errorMsg = "Error Creating Instance of Compiled Assembly (" + filename + "): " + ex.Message; } } } // If no instances were loaded at this point, means we never found types with the ClientInterface if (!loaded && String.IsNullOrEmpty(errorMsg)) { errorMsg = "Expected interface (" + typeof(ClientInterface).ToString() + ") was not found in Compiled Assembly (" + filename + ")"; } } if (!loaded) { // Nothing was loaded, report it AssemblyFailedLoadingEventArgs e = new AssemblyFailedLoadingEventArgs(filename); e.ExtensionType = ExtensionType.Compiled; e.ErrorMessage = errorMsg; this.OnAssemblyFailedLoading(e); } }