Пример #1
0
        public void BindMethod(string symbol, MethodInfo mi, object boundObject)
        {
            var invokeable = new InvokeableItem();

            var fn = new Invokeable()
            {
                ReturnType = this.GetReturnTypeFromMethodInfo(mi),

                Demands = this.GetParameterDemandsFromMethodInfo(mi),

                Function = (space, args) =>
                {
                    if (!args.All(i => ValueItem.IsValueType(i.ItemType)))
                    {
                        throw new Exception("Tried calling bound method with non-value arguments.");
                    }

                    var arguments = args
                                    .Select(i => i as ValueItem)
                                    .Select(i => i.Value)
                                    .ToArray();

                    var result = mi.Invoke(boundObject, arguments);

                    if (result != null)
                    {
                        var resultType = this.GetTypeFromClrType(result.GetType());
                        var resultItem = new ValueItem(resultType, result);
                        return(resultItem);
                    }

                    return(null);
                }
            };

            invokeable.AddInvokeable(fn);
            this.globalSpace.Bind(symbol, invokeable);
        }