示例#1
0
        /// <summary>
        /// Permet l'execution de code compilé
        /// </summary>
        /// <param name="executeContext"></param>
        public static async Task <CompileExecuteResult> ExecuteCodeAsync(ICompiledCode codeModel, params object[] executeParams)
        {
            CompileExecuteResult retour = new CompileExecuteResult();

            retour.TimeExecute = new System.Diagnostics.Stopwatch();
            try
            {
                if (codeModel == null || codeModel.CompiledType == null)
                {
                    throw new Exception("CompiledCode Not Found");
                }
                retour.SourceCodeName = codeModel.SourceCodeName;
                retour.DateExecute    = DateTime.Now;
                retour.TimeExecute.Start();
                object       obj          = Activator.CreateInstance(codeModel.CompiledType);
                BindingFlags bindingFlags = BindingFlags.Default | BindingFlags.InvokeMethod;
                codeModel.CompiledType.InvokeMember(codeModel.CallMasterMethod, bindingFlags, null,
                                                    obj, executeParams);

                return(retour);
            }
            catch (Exception ex)
            {
                throw new Exception("Execute " + ex.Message, ex);
            }
            finally
            {
                retour.TimeExecute.Stop();
            }
        }
示例#2
0
        public bool BuildCode()
        {
            if (inputBox.Text != "")
            {
                statusLabel.Text = "Status: Building...";
                Application.DoEvents();

                string[] refs;
                string code = ParseCode(inputBox.Text, out refs);
                if (code == null || code == "")
                    return false;

                try
                {
                    string input = "DLModMenu " + hTag.Replace(" ", "") + "  ";
                    string[] args = input.Split(' ');
                    //namespace, class, function, static, params
                    CompiledCode = cc.ExecuteCode(ctlMain.AssemblyDirectory, code, args[0], args[1], args[2], ctlMain.cch, refs);
                    statusLabel.Text = "Status: Built";
                    return true;
                }
                catch (Exception ex)
                {
                    //Console.WriteLine("   " + ex.Message);
                    MessageBox.Show(ex.Message);
                    statusLabel.Text = "Status: Error in Code";
                    return false;
                }
            }

            return false;
        }
示例#3
0
        public void ExecuteCommand(string code, IScriptModule module)
        {
            CommandDispatcher dispatcher = ScriptDomainManager.CurrentManager.GetCommandDispatcher();

            if (dispatcher != null)
            {
                Exception     exception     = null;
                ICompiledCode compiled_code = CompileInteractiveCode(code, module);
                if (compiled_code != null)   // TODO: should throw?

                {
                    CallTarget0 run_code = delegate() {
                        try {
                            PrintInteractiveCodeResult(compiled_code.Evaluate(module));
                        } catch (Exception e) {
                            exception = e;
                        }
                        return(null);
                    };

                    dispatcher(run_code);

                    // We catch and rethrow the exception since it could have been thrown on another thread
                    if (exception != null)
                    {
                        throw exception;
                    }
                }
            }
            else
            {
                ExecuteInteractiveCode(code, module);
            }
        }
示例#4
0
        public void ExecuteInteractiveCode(string code, IScriptModule module)
        {
            ICompiledCode cc = CompileInteractiveCode(code, module);

            if (cc != null)
            {
                PrintInteractiveCodeResult(cc.Evaluate(module));
            }
        }
        public ICompiledCode ExecuteCode(string exepath, string code, string namespacename, string classname,
                                         string functionname, ICompiledCodeHost host, string[] refs, params object[] args)
        {
            if (!Directory.Exists(exepath + "\\OutputCS\\"))
            {
                Directory.CreateDirectory(exepath + "\\OutputCS\\");
            }

            string path = Path.GetTempFileName() + "_" + classname;
            string name = exepath + "\\OutputCS\\" + classname + ".cs";

            File.WriteAllText(name, code);
            Assembly asm = BuildAssembly(code, exepath, refs, path, name);

            foreach (Type pluginType in asm.GetTypes())
            {
                if (pluginType.IsPublic)        //Only look at public types
                {
                    if (!pluginType.IsAbstract) //Only look at non-abstract types
                    {
                        //Gets a type object of the interface we need the plugins to match
                        Type typeInterface = pluginType.GetInterface("dhx.ICompiledCode", true);

                        if (typeInterface != null)
                        {
                            ICompiledCode instance = null;
                            //Type type = asm.GetType(namespacename + "." + classname);
                            //object inst = asm.CreateInstance(namespacename + "." + classname);
                            //instance = (ICompiledCode)asm.CreateInstance(namespacename + "." + classname);
                            instance      = (ICompiledCode)Activator.CreateInstance(pluginType);
                            instance.host = host;

                            if (functionname != null && functionname != "")
                            {
                                MethodInfo method = pluginType.GetMethod(functionname);
                                if (method != null)
                                {
                                    method.Invoke(instance, args);
                                }
                            }

                            return(instance);
                        }
                    }
                }
            }

            return(null);
        }