示例#1
0
        public dynamic ListSetup(params dynamic[] constructorArgs)
        {
            var tActivate = constructorArgs.OfType <Activate>().SingleOrDefault();


            if (tActivate == null)
            {
                if (!_buildType.TryGetValue("Object", out tActivate))
                {
                    tActivate = null;
                }
                if (tActivate != null)
                {
                    tActivate = new Activate(tActivate.Type, constructorArgs);
                }
                if (tActivate == null)
                {
                    tActivate = new Activate <ImpromptuList>(constructorArgs);
                }
            }

            _buildType["List"]  = tActivate;
            _buildType["Array"] = tActivate;
            return(this);
        }
示例#2
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));
        }
示例#3
0
 /// <summary>
 /// Sets up object builder
 /// </summary>
 /// <param name="constructorArgs">The constructor args.</param>
 /// <returns></returns>
 public dynamic ObjectSetup(params dynamic[] constructorArgs)
 {
     _buildType["Object"] = new Activate <TObjectProtoType>(constructorArgs);
     return(this);
 }