public IEnumerable <TObject> CreateCollection <TObject>(IEnumerable <dynamic> dataCollection)
            where TObject : class
        {
            List <TObject> collection = new List <TObject>();

            IDictionary <string, object> unwrappedData;

            foreach (var data in dataCollection)
            {
                unwrappedData = data;

                TObject instance = CustomActivator.CreateInstanceAndPopulate <TObject>(unwrappedData);
                collection.Add(instance);
            }

            return(collection);
        }
예제 #2
0
        public override bool TryConvert(ConvertBinder binder, out object result)
        {
            bool flag = false;

            if (binder.Type == typeof(XmlDocument) &&
                !binder.Type.IsAbstract)
            {
                result = ConversionsEngine.CreateXmlDocument(_innerData);
                return(true);
            }

            if (!binder.Type.IsAbstract &&
                !typeof(ICollection).IsAssignableFrom(binder.Type) &&
                !typeof(IEnumerable).IsAssignableFrom(binder.Type) &&
                _innerData.Count != 0)
            {
                try
                {
                    dynamic dynamicData = this.InnerObjects.FirstOrDefault();

                    if (dynamicData != null)
                    {
                        IDictionary <string, object> objData = dynamicData;
                        result = CustomActivator.CreateInstanceAndPopulate(binder.Type, objData);
                    }
                    else
                    {
                        result = CustomActivator.CreateInstanceAndPopulate(binder.Type, _innerData);
                    }

                    flag = true;
                }
                catch (InvalidCastException e)
                {
                    result = null;
                }
                catch (Exception e)
                {
                    result = null;
                }

                return(flag);
            }
            else if (typeof(IEnumerable).IsAssignableFrom(binder.Type))
            {
                if (binder.Type == typeof(string))
                {
                    result = this.ToString();
                }
                else if (binder.Type == typeof(IDictionary <string, object>))
                {
                    result = _innerData;
                }
                else if (binder.Type.IsGenericType)
                {
                    Type genericType = binder.Type.GenericTypeArguments[0];

                    if (genericType == typeof(XmlDocument))
                    {
                        List <XmlDocument> collection = new List <XmlDocument>();

                        foreach (var entry in _innerData)
                        {
                            collection.Add(ConversionsEngine.CreateXmlDocument(entry));
                        }

                        result = collection;
                    }
                    else
                    {
                        result = ConversionsEngine.CreateCollection(genericType, InnerObjects);
                    }
                }
                else if (binder.Type.BaseType == typeof(Array))
                {
                    result = ConversionsEngine.CreateArray(binder.Type.GetElementType(), InnerObjects);
                }
                else
                {
                    result = null;
                    return(false);
                }

                return(true);
            }

            throw new InvalidCastException("Could not cast to type which is not implementing IAppSettings");
        }