AjScript.Language.Function.Invoke C# (CSharp) Method

Invoke() public method

public Invoke ( IContext context, object @this, object arguments ) : object
context IContext
@this object
arguments object
return object
        public virtual object Invoke(IContext context, object @this, object[] arguments)
        {
            // TODO review: any case for use context parameter?
            IContext newctx = new Context(this.context);

            // Set this and arguments
            newctx.DefineVariable("this");
            newctx.SetValue("this", @this);
            newctx.DefineVariable("arguments");
            newctx.SetValue("arguments", arguments);

            for (int k = 0; arguments != null && k < arguments.Length && k < this.parameterNames.Length; k++)
            {
                newctx.DefineVariable(this.parameterNames[k]);
                newctx.SetValue(this.parameterNames[k], arguments[k]);
            }

            // TODO Review: it should be null?
            if (this.Body != null)
                this.Body.Execute(newctx);

            // TODO Review: return undefined it not this?
            if (newctx.ReturnValue == null)
                if (@this != null)
                    return @this;
                else
                    return Undefined.Instance;

            return newctx.ReturnValue.Value;
        }

Usage Example

Example #1
0
        public void EvaluateWithThisObject()
        {
            Context context = new Context();
            ICommand body = new ReturnCommand(new ArithmeticBinaryExpression(ArithmeticOperator.Add, new VariableExpression("x"), new DotExpression(new VariableExpression("this"), "Age")));
            Function function = new Function(new string[] { "x" }, body);
            DynamicObject person = new DynamicObject();
            person.SetValue("Age", 30);

            Assert.AreEqual(40, function.Invoke(context, person, new object[] { 10 }));
        }
All Usage Examples Of AjScript.Language.Function::Invoke