private static void InvokeReturnlessOneParameterMethod(Type type, object obj, string name, object input)
        {
            IActionCallback <object, object> methodActivator = ReflectionUtils.CreateReturnlessOneParameterMethodCallback(type, type.GetMethod(name));

            methodActivator.Invoke(obj, input);
        }
        private static void SetPropertyValue(Type type, object obj, string name, object input)
        {
            IActionCallback <object, object> setAccessor = ReflectionUtils.CreateSetAccessor(type, type.GetProperty(name));

            setAccessor.Invoke(obj, input);
        }
Beispiel #3
0
            /// <summary>
            /// Creates a new writer for the <see cref="ICollection"></see>.
            /// </summary>
            /// <param name="formatter"> The formatter. </param>
            /// <param name="enumerable"> The collection. </param>
            public CollectionWriter(AbstractFormatter formatter, IEnumerable enumerable)
            {
                // Gets type and generic type definition of the collection

                Type collectionType = enumerable.GetType();
                Type elementType    = collectionType.GetGenericArguments()[0];
                Type collectionGTD  = collectionType.GetGenericTypeDefinition();

                // Gets the methods of the collection

                MethodInfo methodInfo = null;

                MethodInfo[] methodsInfo = collectionType.GetMethods(BindingFlags.Instance | BindingFlags.Public);

                Func <string, Action <object> > getCallbackReturnMethod = (name) =>
                {
                    // Builds a callback for the public method with specified name
                    // (one parameter, return something)

                    Func <MethodInfo, bool> predicate = (m) =>
                    {
                        if (m.Name != name || m.ReturnType == voidType)
                        {
                            return(false);
                        }

                        ParameterInfo[] parameters = m.GetParameters();
                        return(parameters.Length == 1 && parameters[0].ParameterType.IsBaseTypeOrEquals(elementType));
                    };

                    methodInfo = methodsInfo.FindOrDefault(predicate);
                    IFunctionCallback <object, object, object> callback = formatter.CreateReturnOneParameterMethodCallback(collectionType, methodInfo);
                    return((o) => callback.Invoke(enumerable, o));
                };

                Func <string, Action <object> > getCallbackReturnlessMethod = (name) =>
                {
                    // Builds a callback for the public method with specified name
                    // (one parameter, returnless)

                    Func <MethodInfo, bool> predicate = (m) =>
                    {
                        if (m.Name != name || m.ReturnType != voidType)
                        {
                            return(false);
                        }

                        ParameterInfo[] parameters = m.GetParameters();
                        return(parameters.Length == 1 && parameters[0].ParameterType.IsBaseTypeOrEquals(elementType));
                    };

                    methodInfo = methodsInfo.FindOrDefault(predicate);
                    IActionCallback <object, object> callback = formatter.CreateReturnlessOneParameterMethodCallback(collectionType, methodInfo);
                    return((o) => callback.Invoke(enumerable, o));
                };

                // Finds the specific methods for the adding a new values of the different collections

                if (collectionGTD == linkedListType)
                {
                    // Linked list
                    this.collectionAddAction = getCallbackReturnMethod("AddLast");
                }

                if (collectionGTD == listType)
                {
                    // List
                    IList list = enumerable as IList;
                    this.collectionAddAction = (o) => list.Add(o);
                }

                if (collectionGTD == hashSetType || collectionGTD == sortedSetType)
                {
                    // Hash set, sorted set
                    this.collectionAddAction = getCallbackReturnMethod("Add");
                }

                if (collectionGTD == concurrentBagType)
                {
                    // Concurrent bag
                    this.collectionAddAction = getCallbackReturnlessMethod("Add");
                }

                if (collectionGTD == queueType || collectionGTD == concurrentQueueType)
                {
                    // Queue, concurrent queue
                    this.collectionAddAction = getCallbackReturnlessMethod("Enqueue");
                }

                if (collectionGTD == stackType || collectionGTD == concurrentStackType)
                {
                    // Stack, concurrent stack
                    this.collectionAddAction = getCallbackReturnlessMethod("Push");
                    this.collectionIsStack   = true;
                }

                // Saves the callback for the found method

                if (this.collectionIsStack)
                {
                    this.addAction = (v) => collectedValues.AddFirst(v);
                }
                else
                {
                    this.addAction = this.collectionAddAction;
                }
            }