Пример #1
0
        public void Test1()
        {
            var foobar = new Expando();

            foobar.Add("foo", "$bar");
            Assert.AreEqual("foo = $bar", foobar.ToString());
            var quux = new Expando();

            quux.Add("qu", "ux");
            Assert.AreEqual("qu = ux", quux.ToString());

            // init
            dynamic map = new Expando();

            Assert.AreEqual("", map.ToString());

            // binaryoperation
            map += foobar;
            map += quux;
            Assert.AreEqual("foo = $bar, qu = ux", map.ToString());

            // fallback to csharpbinder
            map -= quux;
            Assert.AreEqual("foo = $bar", map.ToString());

            // convert
            var s = (String)map;

            Assert.AreEqual("foo = $bar", s);

            // getindex
            var bar = map["foo"];

            Assert.AreEqual("$bar", bar);

            // invoke, fallback to idynamicobject
            map = map(bar: "bar");
            Assert.AreEqual("foo = bar", map.ToString());

            // setindex
            map["two"] = 2;
            Assert.AreEqual("foo = bar, two = 2", map.ToString());
            map.Remove("two");

            //invokemember
            map["fn"] = ((Func <int, int, int>)((x, y) => x + y));
            var z = map.fn(1, 2);

            Assert.AreEqual(3, z);

            // setmember
            map.fn  = "fn";
            map.two = "2";
            Assert.AreEqual("foo = bar, fn = fn, two = 2", map.ToString());

            // unaryoperation
            map = !map;
            Assert.AreEqual("oof = bar, nf = fn, owt = 2", map.ToString());
        }
Пример #2
0
        public static Expando operator !(Expando e)
        {
            if (e == null)
            {
                return(null);
            }
            var result = new Expando();

            e.Keys.ForEach(k => result.Add(new String(k.Reverse().ToArray()), e[k]));
            return(result);
        }
Пример #3
0
        public static Expando operator +(Expando e1, Expando e2)
        {
            if (e1 == null && e2 == null)
            {
                return(null);
            }
            e1 = e1 ?? new Expando();
            e2 = e2 ?? new Expando();
            var result = new Expando(e1);

            e2.Keys.ForEach(k => result.Add(k, e2[k]));
            return(result);
        }
Пример #4
0
        public Object Invoke(InvokeBinder binder, Object[] args)
        {
            var map    = args.ToDictionary((arg, i) => binder.CallInfo.ArgumentNames[i], arg => arg);
            var result = new Expando();

            this.Keys.ForEach(k =>
            {
                var v = this[k];
                var s = (v as String).Extract(@"^\$(?<key>.*)$");
                if (s != null && map.ContainsKey(s))
                {
                    v = map[s];
                }
                result.Add(k, v);
            });

            return(result);
        }