示例#1
0
        public void TestSetDynamicAllAnonymous()
        {
            dynamic tExpando = new ExpandoObject();

            Impromptu.InvokeSetAll(tExpando, new{ One = 1, Two = 2, Three = 3 });


            Assert.AreEqual(1, tExpando.One);
            Assert.AreEqual(2, tExpando.Two);
            Assert.AreEqual(3, tExpando.Three);
        }
示例#2
0
        public void TestSetDynamicAllNamed()
        {
            dynamic tExpando = new ExpandoObject();

            Impromptu.InvokeSetAll(tExpando, One: 1, Two: 2, Three: 3);


            Assert.AreEqual(1, tExpando.One);
            Assert.AreEqual(2, tExpando.Two);
            Assert.AreEqual(3, tExpando.Three);
        }
示例#3
0
        private static object InvokeHelper(CallInfo callinfo, IList <object> args, Activate buildType = null)
        {
            bool   tSetWithName = true;
            object tArg         = null;

            if (callinfo.ArgumentNames.Count == 0 && callinfo.ArgumentCount == 1)
            {
                tArg = args[0];

                if (Util.IsAnonymousType(tArg) || tArg is IEnumerable <KeyValuePair <string, object> > )
                {
                    tSetWithName = false;
                }
            }

            if (tSetWithName && callinfo.ArgumentNames.Count != callinfo.ArgumentCount)
            {
                throw new ArgumentException("Requires argument names for every argument");
            }
            object result;

            if (buildType != null)
            {
                result = buildType.Create();
            }
            else
            {
                try
                {
                    result = Activator.CreateInstance <TObjectProtoType>();//Try first because faster but doens't work with optional parameters
                }
                catch (MissingMethodException)
                {
                    result = Impromptu.InvokeConstructor(typeof(TObjectProtoType));
                }
            }
            if (tSetWithName)
            {
                tArg = callinfo.ArgumentNames.Zip(args, (n, a) => new KeyValuePair <string, object>(n, a));
            }

            return(Impromptu.InvokeSetAll(result, tArg));
        }
示例#4
0
        public void TestSetDynamicAllDict()
        {
            var     tSetValue = "1";
            dynamic tExpando  = new ExpandoObject();

            tExpando.Test       = new ExpandoObject();
            tExpando.Test.Test2 = new ExpandoObject();


            Impromptu.InvokeSetAll(tExpando, new Dictionary <string, object> {
                { "Test.Test2.Test3", tSetValue }, { "One", 1 }, { "Two", 2 }
            });

            Impromptu.InvokeSetChain(tExpando, "Test.Test2.Test3", tSetValue);

            Assert.AreEqual(tSetValue, tExpando.Test.Test2.Test3);
            Assert.AreEqual(1, tExpando.One);
            Assert.AreEqual(2, tExpando.Two);
        }