public dynamic CallFunction(string method, params dynamic[] arguments) { try { ObjectOperations ops = this.mScriptEngine.Operations; if (this.mScriptClass != null) { if (ops.ContainsMember(this.mScriptClass, method)) { object instance = ops.GetMember(this.mScriptClass, method); if (instance != null) { return(ops.InvokeMember(this.mScriptClass, method, arguments)); } } else { this.mLogger.Error("Missing member when trying to execute method '{0}'.", method); this.mScriptLogger.Error("Missing member when trying to execute method '{0}'.", method); } } return(null); } catch (Exception er) { this.mLogger.Error(er, "Script exception in method '{0}'.", method); this.mScriptLogger.Error(er, "Script exception in method '{0}'.", method); throw er; } }
public override void HandleMethodCall(MethodCall method_call) { //object retVal = obj.GetType ().InvokeMember (method_call.Member, BindingFlags.InvokeMethod, null, obj, new object[0]); //IDynamicMetaObjectProvider idyn = obj as IDynamicMetaObjectProvider; object retVal = null; Exception raisedException = null; try { object[] args = MessageHelper.GetDynamicValues(method_call.message); retVal = ops.InvokeMember(obj, method_call.Member, args); //retVal = ops.Call (ops.GetMember (obj, method_call.Member), args); } catch (Exception e) { raisedException = e; } if (!method_call.message.ReplyExpected) { return; } Message msg = method_call.message; Message replyMsg = null; if (raisedException == null) { MethodReturn method_return = new MethodReturn(msg.Header.Serial); replyMsg = method_return.message; if (retVal != null) { if (retVal.GetType().FullName == "IronRuby.Builtins.MutableString") { retVal = retVal.ToString(); } // TODO: Invalid sig handling Signature outSig = Signature.GetSig(retVal.GetType()); MessageWriter retWriter = new MessageWriter(); retWriter.Write(retVal.GetType(), retVal); //retWriter.WriteValueType (retVal, retVal.GetType ()); replyMsg.Body = retWriter.ToArray(); replyMsg.Signature = outSig; } } else { Error error = method_call.CreateError(Mapper.GetInterfaceName(raisedException.GetType()), raisedException.Message); replyMsg = error.message; } if (method_call.Sender != null) { replyMsg.Header[FieldCode.Destination] = method_call.Sender; } conn.Send(replyMsg); }
//-/////////////////////////////////////////////////////////////////////// //-/////////////////////////////////////////////////////////////////////// private void TickPython(Graphics g) { if (_scriptEngine == null) { return; } _pydoodleModule.BeginDraw(g, GraphicsPanel.GraphicsControl); try { switch (_scriptState) { case ScriptState.RunGlobal: { _scriptSource.Execute(); _scriptState = ScriptState.RunTick; } break; case ScriptState.RunTick: { if (_runPyobj != null) { //_textPanel.ClearText(); _pydoodleModule.GraphicsControl.ResetHandlesList(); _objectOperations.InvokeMember(_runPyobj, "tick"); _textPanel.ClearOnNextText = true; } } break; } } catch (Exception e) { _scriptException = e; _scriptState = ScriptState.Borked; _textPanel.AppendText("Exception: " + e.Message + Environment.NewLine); _textPanel.AppendText(Misc.GetScriptExceptionDynamicStackFramesTrace(e)); _graphicsPanel.GraphicsControl.ResetHandlesList(); } _pydoodleModule.EndDraw(); }
/// <summary> /// Calls the method. /// </summary> /// <param name="receiver">The receiver.</param> /// <param name="message">The message.</param> /// <param name="args">The args.</param> /// <returns></returns> public object CallMethod(object receiver, string message, params object[] args) { return(Operations.InvokeMember(receiver, GetMethodName(receiver, message), args)); }
public static void try5(string[] args) { ScriptEngine pyEngine = Python.CreateEngine(); dynamic pyScope = pyEngine.CreateScope(); ScriptSource source = pyEngine.CreateScriptSourceFromFile("CdiClient.py"); source.Compile().Execute(pyScope); ObjectOperations ops = pyEngine.CreateOperations(pyScope); // create member objects for worker object: watermark provider, tracer, logger WatermarkProvider watermarkProvider = new WatermarkProvider(); pyScope.SetVariable("watermarkProvider", watermarkProvider); dynamic cdiWorker = pyEngine.Execute("CdiClient()", pyScope); // create an instance of the worker obj // TODO -- check that all of the right entrypoints are available. Console.WriteLine("static function doc: {0}", pyScope.SampleStaticFunction.__doc__); Console.WriteLine("indirect class doc: {0}", pyScope.CdiClient.__doc__); Console.WriteLine("direct class doc: {0}", cdiWorker.__doc__); Console.WriteLine("class function doc: {0}", cdiWorker.ProcessOneEvent.__doc__); //CheckForEntryPoint(); Console.WriteLine("NoOp: {0}", ops.ContainsMember(cdiWorker, "NoOp")); Console.WriteLine("FooBar: {0}", ops.ContainsMember(cdiWorker, "FooBar")); IList <string> memberNames = ops.GetMemberNames(cdiWorker); foreach (string s in memberNames) { Console.WriteLine(" names: {0}", s); } IList <string> signatures = ops.GetCallSignatures(cdiWorker); foreach (string s in signatures) { Console.WriteLine(" sig: {0}", s); } dynamic result; result = ops.InvokeMember(cdiWorker, "NoOp", new object[] { "hi mom" }); result = ops.InvokeMember(cdiWorker, "NoOp", new object[] { "hi dad" }); result = ops.InvokeMember(cdiWorker, "NoOp", new object[] { "watch this" }); for (int i = 0; i < 10; i++) { MockEHEvent ehEvent = new MockEHEvent(); WatermarkData wmData = new WatermarkData(); System.Threading.Thread.Sleep(args.Length > 0 ? int.Parse(args[0]) : 100); result = ops.InvokeMember(cdiWorker, "ProcessOneEvent", new object[] { ehEvent, wmData }); } WatermarkDelegate del = delegate() { Console.WriteLine("delegate"); }; result = ops.InvokeMember(cdiWorker, "ProcessDelegate", new object[] { del }); WatermarkDelegateWithVal delVal = delegate(string s) { Console.WriteLine("delegate {0}", s); }; result = ops.InvokeMember(cdiWorker, "ProcessDelegateWithVal", new object[] { delVal }); }