public void TestMethodDispatcherWithSingleMethod() { var obj = new Elephant(); var dispatcher = new MethodDispatcher(); dispatcher.AddMethod(typeof(Elephant).Methods("Eat").First()); dispatcher.Invoke(obj, true, new {}); Assert.AreEqual(1, obj.MethodInvoked); }
public void MethodInfoProperty() { // Arrange MethodInfo original = typeof(object).GetMethod("ToString"); MethodDispatcher dispatcher = new MethodDispatcher(original); // Act MethodInfo returned = dispatcher.MethodInfo; // Assert Assert.AreSame(original, returned); }
public static bool Receive(MethodProxy resultProxy, bool socketSuccess) { try { var taskToReturn = MethodDispatcher.GetTaskDispatcher(resultProxy.TaskIdentity); if (taskToReturn == null) { return(false); } if (socketSuccess && resultProxy.TaskSuccess) { MethodDispatcher.SetTaskResult(resultProxy.TaskIdentity, resultProxy); } else { Exception exception = null; //If success value (from javascript) is false, like unable to connect to websocket //or if the native task failed with an exception, cancel the current task, that will throw if (!socketSuccess) { exception = new InvalidOperationException($"BlazorMobile was unable to connect to native through websocket server to execute task {resultProxy.TaskIdentity}"); } else if (resultProxy.ExceptionDescriptor != null) { //We have some message to send in this case exception = new Exception(resultProxy.ExceptionDescriptor.Message); } else { //Sending uncustomized message exception = new InvalidOperationException($"Task {resultProxy.TaskIdentity} has thrown an exception on native side. See log for more info."); } MethodDispatcher.SetTaskAsFaulted(resultProxy.TaskIdentity, exception); } taskToReturn.RunSynchronously(); //Clear task from task list. Should then call the task to execute. It will throw if it has been cancelled MethodDispatcher.ClearTask(resultProxy.TaskIdentity); } catch (Exception ex) { ConsoleHelper.WriteException(ex); return(false); } return(true); }
public void TestMethodDispatcherWithStaticMethods() { var type = typeof(StaticElephant); var dispatcher = new MethodDispatcher(); typeof(StaticElephant).Methods(Flags.StaticAnyVisibility).ForEach(dispatcher.AddMethod); dispatcher.Invoke(type, true, new {}); Assert.AreEqual(1, StaticElephant.MethodInvoked); dispatcher.Invoke(type, true, new { count = 2.0, food = "hay", isHay = true }); Assert.AreEqual(5, StaticElephant.MethodInvoked); dispatcher.Invoke(type, false, new { count = 2, volume = 4 }); Assert.AreEqual(3, StaticElephant.MethodInvoked); }
public void TestMethodDispatcherWithMultipleMethods() { var obj = new Elephant(); var dispatcher = new MethodDispatcher(); typeof(Elephant).Methods(Flags.InstanceAnyVisibility | Flags.ExcludeBackingMembers).ForEach(dispatcher.AddMethod); dispatcher.Invoke(obj, true, new {}); Assert.AreEqual(1, obj.MethodInvoked); dispatcher.Invoke(obj, true, new { count = 2.0, food = "hay", isHay = true }); Assert.AreEqual(5, obj.MethodInvoked); dispatcher.Invoke(obj, true, new { count = 2, volume = 4 }); Assert.AreEqual(11, obj.MethodInvoked); }
public void GetDispatcher() { // Arrange MethodInfo methodInfo = typeof(object).GetMethod("ToString"); MethodDispatcherCache cache = new MethodDispatcherCache(); // Act MethodDispatcher dispatcher1 = cache.GetDispatcher(methodInfo); MethodDispatcher dispatcher2 = cache.GetDispatcher(methodInfo); // Assert Assert.AreSame(methodInfo, dispatcher1.MethodInfo); Assert.AreSame(dispatcher1, dispatcher2, "Dispatcher was not correctly cached."); }
public void ExecuteWithStaticMethod() { // Arrange MethodContainer container = new MethodContainer(); object[] parameters = new object[0]; MethodInfo methodInfo = typeof(MethodContainer).GetMethod("StaticMethod"); MethodDispatcher dispatcher = new MethodDispatcher(methodInfo); // Act object returnValue = dispatcher.Execute(container, parameters); // Assert Assert.AreEqual(89, returnValue); }
public void ExecuteWithVoidInstanceMethod() { // Arrange MethodContainer container = new MethodContainer(); object[] parameters = new object[] { 5, "some string", new DateTime(2001, 1, 1) }; MethodInfo methodInfo = typeof(MethodContainer).GetMethod("VoidMethod"); MethodDispatcher dispatcher = new MethodDispatcher(methodInfo); // Act object returnValue = dispatcher.Execute(container, parameters); // Assert Assert.IsNull(returnValue); Assert.AreEqual(5, container._i); Assert.AreEqual("some string", container._s); Assert.AreEqual(new DateTime(2001, 1, 1), container._dt); }
private static MSA.LambdaExpression /*!*/ CreateLambda(string /*!*/ name, AstParameters /*!*/ parameters, MSA.Expression /*!*/ body) { var result = MethodDispatcher.CreateRubyMethodLambda(body, name, parameters); if (result != null) { return(result); } // to many parameters for Func<> delegate -> use object[]: MSA.ParameterExpression array = Ast.Parameter(typeof(object[]), "#params"); var actualParameters = new AstParameters() { parameters[0], parameters[1], array }; parameters.RemoveAt(0); parameters.RemoveAt(1); var bodyWithParamInit = new AstExpressions(parameters.Count + 1); for (int i = 0; i < parameters.Count; i++) { bodyWithParamInit[i] = Ast.Assign(parameters[i], Ast.ArrayIndex(array, AstUtils.Constant(i))); } bodyWithParamInit[parameters.Count] = body; return(Ast.Lambda <Func <object, Proc, object[], object> >( Ast.Block( parameters, bodyWithParamInit ), name, actualParameters )); }
/// <summary> /// <para>Create object</para> /// </summary> public AsyncTaskActionDescriptor(MethodInfo actionMethod, ControllerDescriptor controllerDescriptor) { _actionMethod = actionMethod; _actionMethodDispatcher = new MethodDispatcher(actionMethod); _controllerDescriptor = controllerDescriptor; }
public Task <List <string> > DisplayAlert(string title, string msg, string cancel) { return(MethodDispatcher.CallMethodAsync <List <string> >(MethodBase.GetCurrentMethod(), title, msg, cancel)); }
/// <summary> /// Construtor. /// </summary> /// <param name="ca"></param> /// <param name="methodInfo"></param> public MethodTarget(ConfigurationAttribute ca, MethodInfo methodInfo) : base(ca) { dispatcher = new MethodDispatcher(new MethodInvoker(methodInfo, ca.RequiredParameters)); }
public Task CallFaultyTask() { return(MethodDispatcher.CallVoidMethodAsync(MethodBase.GetCurrentMethod())); }