Example #1
0
        static void Main(string[] args)
        {
            DynamicDelegate d = new DynamicDelegate();


            var de = d.CreateDelegate <Action <string, int> >((e) =>
            {
                Console.WriteLine(e.Length);
            });

            de("abc", 1);

            return;

            EventRealProxy proxy = new EventRealProxy(typeof(IHello));

            IHello hello = proxy.GetTransparentProxy() as IHello;

            hello.Hello += Hello_Hello;

            proxy.Raise("Hello", null, null);

            hello.Hello -= Hello_Hello;

            proxy.Raise("Hello", null, null);

            Console.WriteLine();
        }
Example #2
0
        public static Delegate WrapDynamicDelegate(Type delegate_type, DynamicDelegate dynamic_delegate)
        {
            var delegate_parameter_types =
                delegate_type
                .GetMethod("Invoke")
                .GetParameters()
                .Select(p => p.ParameterType)
                .ToArray();
            var event_handler_parameter_expressions =
                delegate_parameter_types
                .Zip(Enumerable.Range(0, delegate_parameter_types.Count()), (type, i) => new { type, name = $"_{i}" })
                .Select(e => Expression.Parameter(e.type, e.name))
                .ToArray();

            // parameter_array = new object[] { _1, _2, ... , _N };
            var parameter_array_expression = Expression.NewArrayInit(typeof(object), event_handler_parameter_expressions);
            // Some magical binding/glue right here
            Expression <DynamicDelegate> dynamic_expression = (objects) => dynamic_delegate(objects);
            // dynamic_delegate(parameter_array)
            var invoke = Expression.Invoke(dynamic_expression, parameter_array_expression);
            // Typed wrapper
            var wrapped = Expression.Lambda(delegate_type, invoke, event_handler_parameter_expressions);

            return(wrapped.Compile());
        }
        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            var method      = DynamicDelegate.From(context.Method);
            var returnValue = method(context.Target, new object[0]);

            var task = returnValue as System.Threading.Tasks.Task;

            if (task != null)
            {
                returnValue = task.AsResult();
            }

            var result = returnValue as IResult;

            if (result != null)
            {
                returnValue = new[] { result };
            }

            var enumerable = returnValue as IEnumerable <IResult>;

            if (enumerable != null)
            {
                returnValue = enumerable.GetEnumerator();
            }

            var enumerator = returnValue as IEnumerator <IResult>;

            if (enumerator != null)
            {
                Coroutine.BeginExecute(enumerator, context);
            }
        }
Example #4
0
        public void Map_Dispatches_To_Transformer_Objects()
        {
            var             listXf = new _ListXF();
            object          res    = R.Map(add1, listXf);
            DynamicDelegate f      = (DynamicDelegate)res.Member("f", @private: true);
            object          xf     = res.Member("xf", @private: true);

            Assert.IsInstanceOfType(res, typeof(XMap));
            Assert.AreEqual(xf, listXf);
            Assert.AreEqual(f.Unwrap(), add1);
        }
Example #5
0
    private void AddDynamicDelegate(Delegate dele, bool append)
    {
        if (delayProcesList == null)
        {
            delayProcesList = new List <DynamicDelegate>();
        }

        DynamicDelegate dd = new DynamicDelegate
        {
            append   = append,
            callback = dele
        };

        delayProcesList.Add(dd);
    }
        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            var target = targetReference.Target;

            if (target == null)
            {
                return;
            }

            var execute     = DynamicDelegate.From(method);
            var returnValue = execute(target, new object[0]);

            var task = returnValue as System.Threading.Tasks.Task;

            if (task != null)
            {
                returnValue = task.AsResult();
            }

            var result = returnValue as IResult;

            if (result != null)
            {
                returnValue = new[] { result };
            }

            var enumerable = returnValue as IEnumerable <IResult>;

            if (enumerable != null)
            {
                returnValue = enumerable.GetEnumerator();
            }

            var enumerator = returnValue as IEnumerator <IResult>;

            if (enumerator != null)
            {
                var context = new CoroutineExecutionContext
                {
                    Target = target,
                };

                Coroutine.BeginExecute(enumerator, context);
            }
        }
Example #7
0
        public void Map_Can_Compose_Transducer_Style()
        {
            var             listXf   = new _ListXF();
            var             mdouble  = R.Map(times2);
            var             mdec     = R.Map(dec);
            var             xcomp    = mdec(mdouble(listXf));
            object          objxcomp = xcomp;
            var             xmap     = objxcomp.Member("xf", @private: true);
            DynamicDelegate f        = (DynamicDelegate)objxcomp.Member("f", @private: true);
            DynamicDelegate xmapf    = (DynamicDelegate)xmap.Member("f", @private: true);
            object          xmapXf   = xmap.Member("xf", @private: true);

            Assert.IsInstanceOfType(xcomp, typeof(XMap));
            Assert.IsInstanceOfType(xmap, typeof(XMap));
            Assert.AreEqual(xmapXf, listXf);
            Assert.AreEqual(xmapf.Unwrap(), times2);
            Assert.AreEqual(f.Unwrap(), dec);
        }
 public Delegate CreateDelegate(DynamicDelegate dele)
 {
     return(MethodFactory.CreateDelegate(m_DelegateType, dele));
 }