Example #1
0
        private static object InvokeHelper(CallInfo callinfo, IList <object> args, Activate buildType = null)
        {
            bool   setWithName = true;
            object theArgument = null;

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

                if (TypeFactorization.IsTypeAnonymous(theArgument) ||
                    theArgument is IEnumerable <KeyValuePair <string, object> > )
                {
                    setWithName = false;
                }
            }

            if (setWithName && 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>();
                }
                catch (MissingMethodException)
                {
                    result = InvocationBinding.CreateInstance(typeof(TObjectProtoType));
                }
            }
            if (setWithName)
            {
                theArgument = callinfo.ArgumentNames.Zip(args, (n, a) => new KeyValuePair <string, object>(n, a));
            }

            return(InvocationBinding.InvokeSetAll(result, theArgument));
        }
Example #2
0
        public override bool TryInvoke(InvokeBinder binder, object[] args, out object result)
        {
            IEnumerable <KeyValuePair <string, object> > kvps = null;
            object target = null;

            result = null;

            //Setup Properties as dictionary
            if (binder.CallInfo.ArgumentNames.Any())
            {
                if (binder.CallInfo.ArgumentNames.Count + 1 == binder.CallInfo.ArgumentCount)
                {
                    target = args.First();
                    kvps   = binder.CallInfo
                             .ArgumentNames
                             .Zip(args.Skip(1), (key, value) => new { key, value })
                             .ToDictionary(k => k.key, v => v.value);
                }
                else
                {
                    throw new RuntimeBinderException(
                              "InvokeSetAll requires first parameter to be target unamed, and all other parameters to be named.");
                }
            }
            else if (args.Length == 2)
            {
                target = args[0];
                if (args[1] is IEnumerable <KeyValuePair <string, object> > )
                {
                    kvps = (IEnumerable <KeyValuePair <string, object> >)args[1];
                }
                else if (args[1] is IEnumerable &&
                         args[1].GetType().IsGenericType
                         )
                {
                    var enArgs = (IEnumerable)args[1];

                    var tInterface = enArgs.GetType().GetInterface("IEnumerable`1", false);
                    if (tInterface != null)
                    {
                        var tParamTypes = tInterface.GetGenericArguments();
                        if (tParamTypes.Length == 1 &&
                            tParamTypes[0].GetGenericTypeDefinition() == typeof(Tuple <,>))
                        {
                            kvps = enArgs.Cast <dynamic>().ToDictionary(k => (string)k.Item1, v => v.Item2);
                        }
                    }
                }
                else if (TypeFactorization.IsTypeAnonymous(args[1]))
                {
                    var keyDict = new Dictionary <string, object>();
                    foreach (var tProp in args[1].GetType().GetProperties())
                    {
                        keyDict[tProp.Name] = InvocationBinding.InvokeGet(args[1], tProp.Name);
                    }
                    kvps = keyDict;
                }
            }
            //Invoke all properties
            if (target != null && kvps != null)
            {
                foreach (var pair in kvps)
                {
                    InvocationBinding.InvokeSetChain(target, pair.Key, pair.Value);
                }
                result = target;
                return(true);
            }
            return(false);
        }