LynnaLab.PluginCore.Compile C# (CSharp) Method

Compile() private method

private Compile ( string code, string filename ) : Type
code string
filename string
return System.Type
        Type Compile(string code, string filename)
        {
            Type pluginType = null;

            var CompilerParams = new CompilerParameters();

            CompilerParams.GenerateInMemory = true;
            CompilerParams.TreatWarningsAsErrors = false;
            CompilerParams.GenerateExecutable = false;
            CompilerParams.CompilerOptions = "/optimize";

            CompilerParams.ReferencedAssemblies.AddRange(referencedAssemblies);

            var provider = new CSharpCodeProvider();
            CompilerResults compile = provider.CompileAssemblyFromSource(CompilerParams, code);

            if (compile.Errors.HasErrors) {
                string text = "Compile error: ";
                foreach (CompilerError ce in compile.Errors)
                {
                    text += ce.ToString() + '\n';
                }
                Gtk.MessageDialog d = new MessageDialog(null,
                        DialogFlags.DestroyWithParent,
                        MessageType.Error,
                        ButtonsType.Ok,
                        "There was an error compiling \"" + filename + "\":\n\n" + text);
                d.Run();
                d.Destroy();
                return null;
            }

            foreach (Module module in compile.CompiledAssembly.GetModules()) {
                foreach (Type type in module.GetTypes()) {
                    if (type.BaseType == typeof(Plugin)) {
                        Console.WriteLine(type + " implements Plugin");
                        pluginType = type;
                        break;
                    }
                }
                if (pluginType != null)
                    break;
            }

            return pluginType;
        }