示例#1
0
        /// <summary>
        /// Create a dynamic collection of Bson documents.
        /// </summary>
        /// <param name="documents">The collection of Bson documents.</param>
        /// <param name="collectionName">The name of the collection.</param>
        /// <returns>The collection of dynamic models.</returns>
        public virtual object[] CreateDynamicModel(IEnumerable <BsonDocument> documents, string collectionName = "MongoDBCollection")
        {
            List <object> result = new List <object>();

            // Make sure documents exits.
            if (documents != null && documents.Count() > 0)
            {
                // Create the dynamic type builder.
                Nequeo.Reflection.DynamicTypeBuilder builder = new Reflection.DynamicTypeBuilder("DynamicBsonDocumentModule");

                // For each document.
                foreach (BsonDocument item in documents)
                {
                    int i = 0;

                    // Get the current row.
                    Dictionary <string, object> row = item.ToDictionary();

                    // Get all document property names.
                    List <string> propertyName = new List <string>();
                    foreach (KeyValuePair <string, object> id in row)
                    {
                        // Get the name.
                        string name = id.Key;
                        if (name.ToLower().Contains("_id"))
                        {
                            name = "Id";
                        }

                        // Add the name.
                        propertyName.Add(name);
                    }

                    // Create the dynamic type.
                    Nequeo.Reflection.DynamicPropertyValue[] dynamicTypeProperties = new Nequeo.Reflection.DynamicPropertyValue[row.Count()];

                    // Get the document values.
                    IEnumerable <BsonValue> values = item.Values;

                    // For each Bson value.
                    foreach (BsonValue bsonValue in values)
                    {
                        // Map the Bson type tp .Net type.
                        object netValue = BsonTypeMapper.MapToDotNetValue(bsonValue);
                        Nequeo.Reflection.DynamicPropertyValue propertyValue = new Reflection.DynamicPropertyValue(propertyName[i], netValue.GetType(), netValue);

                        // Add the value to the list of dynamic types.
                        dynamicTypeProperties[i] = propertyValue;
                        i++;
                    }

                    // Create the model from the document.
                    object model = builder.Create(collectionName, dynamicTypeProperties);
                    result.Add(model);
                }
            }

            // Return the dynamic array.
            return(result.ToArray());
        }
        /// <summary>
        /// Process the response of xml data to the type array, from a WCF request.
        /// </summary>
        /// <param name="xmlData">The byte array containing the xml data.</param>
        /// <returns>The transformed data type array.</returns>
        public static object[] ProcessResponseEx(byte[] xmlData)
        {
            int           i        = 0;
            List <object> dataList = new List <object>();

            Nequeo.Reflection.DynamicTypeBuilder builder = new Reflection.DynamicTypeBuilder("dynamicModule");

            // Crate the document.
            XmlDocument xmlDoc = Nequeo.Xml.Element.Document(xmlData);

            //Create namespace manager
            XmlNamespaceManager nsmgr = new XmlNamespaceManager(xmlDoc.NameTable);

            foreach (Nequeo.Model.NameValue item in _namespaces)
            {
                // Add the namespace.
                nsmgr.AddNamespace(item.Name, item.Value);
            }

            // Get all the entry nodes from the xml document.
            XmlNodeList propertyNodes = xmlDoc.DocumentElement.GetElementsByTagName(_memberPropertiesElementName);

            foreach (System.Xml.XmlNode propertyNode in propertyNodes)
            {
                // Increment the dymanic object count.
                i++;

                // Create the dynamic types.
                List <Nequeo.Reflection.DynamicPropertyValue> dynamicTypes = new List <Reflection.DynamicPropertyValue>();

                // If properties exist.
                if (propertyNode.HasChildNodes)
                {
                    // Get all the child nodes, these are all the individual properties.
                    XmlNodeList properties = propertyNode.ChildNodes;
                    foreach (System.Xml.XmlNode property in properties)
                    {
                        // Find the property in the property node.
                        XmlNode propertyName = propertyNode.SelectSingleNode(property.Name, nsmgr);
                        if (propertyName != null)
                        {
                            // Get the type.
                            XmlNode propertyTypeNode = propertyName.Attributes.GetNamedItem(_memberTypePropertiesElementName);
                            string  typeName         = propertyTypeNode.Value;
                            Type    type             = Nequeo.DataType.GetSystemType(typeName.ToLower().Replace(_typePrefix.ToLower(), ""));

                            // Create a new property type.
                            Nequeo.Reflection.DynamicPropertyValue dynamicProperty =
                                new Reflection.DynamicPropertyValue(property.Name.TrimStart(_dataPrefix), type, Convert.ChangeType(propertyName.InnerText, type));

                            // Add to the array.
                            dynamicTypes.Add(dynamicProperty);
                        }
                    }
                }

                // Create the dynamic type.
                // Create a new instance of the type.
                object data = builder.Create("" + i.ToString(), dynamicTypes);

                // Add the type to the collection.
                dataList.Add(data);
            }

            // Return the array.
            return(dataList.ToArray());
        }