Example #1
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 (Exception)
                {
                    result = Dynamic.InvokeConstructor(typeof(TObjectProtoType));
                }
            }
            if (tSetWithName)
            {
                tArg = callinfo.ArgumentNames.Zip(args, (n, a) => new KeyValuePair <string, object>(n, a));
            }

            return(Dynamic.InvokeSetAll(result, tArg));
        }