SetValue() публичный Метод

public SetValue ( string name, object value, bool enumerable = true ) : void
name string
value object
enumerable bool
Результат void
Пример #1
0
        public void GetNames()
        {
            DynamicObject dynobj = new DynamicObject();

            dynobj.SetValue("FirstName", "Adam");
            dynobj.SetValue("LastName", "Genesis");

            ICollection<string> names = dynobj.GetNames();

            Assert.IsNotNull(names);
            Assert.AreEqual(2, names.Count);

            Assert.IsTrue(names.Contains("FirstName"));
            Assert.IsTrue(names.Contains("LastName"));
        }
Пример #2
0
        public void ExecuteDeleteCommand()
        {
            DynamicObject dynobj = new DynamicObject();
            dynobj.SetValue("name", "Adam");
            dynobj.SetValue("age", 800);

            DeleteCommand cmd = new DeleteCommand(new DotExpression(new VariableExpression("adam"), "name"));

            var context = new Context();
            context.SetValue("adam", dynobj);

            cmd.Execute(context);

            Assert.IsTrue(dynobj.HasName("age"));
            Assert.IsFalse(dynobj.HasName("name"));
        }
Пример #3
0
        public ObjectFunction(IContext context)
            : base(null, null, context)
        {
            var prototype = new DynamicObject();

            this.SetValue("prototype", prototype);
            prototype.SetValue("toString", toStringFunction);
        }
Пример #4
0
        public ArrayFunction(IContext context)
            : base(null, null, context)
        {
            var prototype = new DynamicObject();

            if (context != null)
            {
                var obj = context.GetValue("Object");

                if (obj != null && obj is DynamicObject)
                {
                    var superproto = ((DynamicObject)obj).GetValue("prototype");
                    if (superproto != null && superproto is DynamicObject)
                        prototype.SetValue("prototype", superproto);
                }
            }

            this.SetValue("isArray", isArrayFunction);
            this.SetValue("prototype", prototype);
            prototype.SetValue("push", pushFunction);
            prototype.SetValue("pop", popFunction);
            prototype.SetValue("unshift", unshiftFunction);
            prototype.SetValue("shift", shiftFunction);
            prototype.SetValue("join", joinFunction);
            prototype.SetValue("slice", sliceFunction);
        }
Пример #5
0
 public void GetValueFromPrototypeAndSetValueInObject()
 {
     DynamicObject prototype = new DynamicObject();
     this.function.SetValue("prototype", prototype);
     prototype.SetValue("x", 10);
     this.dynobj.SetValue("x", 20);
     Assert.AreEqual(20, this.dynobj.GetValue("x"));
     Assert.AreEqual(10, prototype.GetValue("x"));
 }
Пример #6
0
        public object Evaluate(IContext context)
        {
            DynamicObject obj = new DynamicObject((IFunction)context.RootContext.GetValue("Object"));

            for (int k = 0; k < this.names.Count; k++)
                obj.SetValue(this.names[k], this.expressions[k].Evaluate(context));

            return obj;
        }
Пример #7
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 }));
        }
Пример #8
0
 public void GetValueFromPrototypeAndSetValueInObject()
 {
     Function function = new Function(null, null);
     DynamicObject dynobj = new DynamicObject(function);
     DynamicObject prototype = new DynamicObject();
     function.SetValue("prototype", prototype);
     prototype.SetValue("x", 10);
     dynobj.SetValue("x", 20);
     Assert.AreEqual(20, dynobj.GetValue("x"));
     Assert.AreEqual(10, prototype.GetValue("x"));
 }
Пример #9
0
        public override object NewInstance(object[] parameters)
        {
            var dynobj = new DynamicObject(this);

            if (parameters == null || parameters.Length == 0)
                return dynobj;

            var arg = parameters[0];

            if (arg == null)
                arg = "null";
            else
                arg = arg.ToString();

            var str = (string)arg;

            for (int k = 0; k < str.Length; k++)
                dynobj.SetValue(k.ToString(), str[k].ToString());

            dynobj.SetValue("length", str.Length, false);

            return dynobj;
        }
Пример #10
0
        public void DefineMethod()
        {
            DynamicObject dynobj = new DynamicObject();

            ICommand body = new ReturnCommand(new VariableExpression("Name"));
            Function function = new Function(null, body);

            dynobj.SetValue("GetName", function);

            object result = dynobj.GetValue("GetName");

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(ICallable));
            Assert.IsTrue(result == function);
        }
Пример #11
0
        public void EvaluateArrayDotExpression()
        {
            Context context = new Context();

            DynamicObject data = new DynamicObject();
            data.SetValue("Numbers", new string[] { "one", "two", "three" });

            context.SetValue("data", data);

            IExpression expression = new IndexedExpression(new DotExpression(new VariableExpression("data"), "Numbers"), new IExpression[] { new ConstantExpression(1) });

            object result = expression.Evaluate(context);

            Assert.IsNotNull(result);
            Assert.AreEqual("two", result);
        }
Пример #12
0
        public void UsePrototypeForGetValue()
        {
            Function function = new Function(null, null);
            DynamicObject dynobj = new DynamicObject(function);

            DynamicObject prototype = new DynamicObject();
            function.SetValue("prototype", prototype);
            prototype.SetValue("x", 10);

            Assert.AreEqual(10, dynobj.GetValue("x"));
        }
Пример #13
0
        public void SetAndGetValue()
        {
            DynamicObject dynobj = new DynamicObject();

            dynobj.SetValue("Foo", "Bar");

            Assert.AreEqual("Bar", dynobj.GetValue("Foo"));
        }
Пример #14
0
 public void RemoveValue()
 {
     DynamicObject dynobj = new DynamicObject();
     dynobj.SetValue("name", "Adam");
     Assert.IsTrue(dynobj.HasName("name"));
     dynobj.RemoveValue("name");
     Assert.IsFalse(dynobj.HasName("name"));
     Assert.AreSame(Undefined.Instance, dynobj.GetValue("name"));
 }
Пример #15
0
 public void HasName()
 {
     DynamicObject dynobj = new DynamicObject();
     dynobj.SetValue("name", "Adam");
     Assert.IsTrue(dynobj.HasName("name"));
 }
Пример #16
0
        public void InvokeNativeMethod()
        {
            DynamicObject dynobj = new DynamicObject();

            ICommand body = new ReturnCommand(new VariableExpression("Name"));
            Function function = new Function(null, body);

            dynobj.SetValue("Name", "Adam");

            object result = dynobj.Invoke("GetValue", new object[] { "Name" });

            Assert.IsNotNull(result);
            Assert.IsInstanceOfType(result, typeof(string));
            Assert.AreEqual("Adam", result);
        }
Пример #17
0
 public void HasNameInPrototype()
 {
     Function function = new Function(null, null);
     DynamicObject dynobj = new DynamicObject(function);
     DynamicObject prototype = new DynamicObject();
     function.SetValue("prototype", prototype);
     prototype.SetValue("name", "Adam");
     Assert.IsTrue(dynobj.HasName("name"));
 }
Пример #18
0
        public void UsePrototypeForGetValue()
        {
            DynamicObject prototype = new DynamicObject();
            this.function.SetValue("prototype", prototype);
            prototype.SetValue("x", 10);

            Assert.AreEqual(10, this.dynobj.GetValue("x"));
        }