コード例 #1
0
        /// <summary>
        /// Fills the object.
        /// </summary>
        /// <param name="objectToPopulate">The object to populate.</param>
        /// <param name="fields">The fields.</param>
        /// <param name="entityPrefix">The entity prefix.</param>
        public static void FillObject(DiscoveryBusinessObject objectToPopulate, TextFieldCollection fields, string entityPrefix)
        {
            // Qualified property name
            string qualifiedProperty = "";

            // Iterate over the properties of the type
            List <PropertyInfo> objectProperties = TextFieldParser.GetPropertyInfo(objectToPopulate.GetType());

            foreach (PropertyInfo objectProperty in objectProperties)
            {
                // Build the qualified name
                qualifiedProperty = ((string.IsNullOrEmpty(entityPrefix))?"":entityPrefix + ".") + objectProperty.Name;

                // See if we can write to the property
                if (objectProperty.CanWrite)
                {
                    // See if it's a complex type
                    if (objectProperty.PropertyType.IsSubclassOf(typeof(DiscoveryBusinessObject)))
                    {
                        // It's a complext type
                        FillObject(
                            (DiscoveryBusinessObject)objectProperty.GetValue(objectToPopulate, null),
                            fields,
                            qualifiedProperty);
                    }
                    else if (null != fields[qualifiedProperty])
                    {
                        // See if the property is in the text fields collection
                        objectProperty.SetValue(
                            objectToPopulate,
                            Convert.ChangeType(fields[qualifiedProperty].Value, objectProperty.PropertyType), null);
                    }
                }
            }
        }
コード例 #2
0
        /// <summary>
        /// Populates the properties of the provided object using reflection.
        /// </summary>
        /// <remarks>If a property name includes a '.' an attempt will be made to populate the child property</remarks>
        /// <param name="fields">The fields.</param>
        /// <param name="objectToPopulate">The object to populate.</param>
        protected static void PopulateProperties(TextFieldCollection fields, DiscoveryBusinessObject objectToPopulate)
        {
            foreach (TextField field in fields)
            {
                String[]     propertyName = field.Name.Split(new char[] { '.' });
                PropertyInfo property     = objectToPopulate.GetType().GetProperty(propertyName[0]);
                if (property != null)
                {
                    object parentObject = objectToPopulate;
                    if (propertyName.Length == 2)
                    {
                        //crappy code, should be recursive but it's a start when deailing with complex custom types!
                        parentObject = property.GetValue(objectToPopulate, null);
                        property     = parentObject.GetType().GetProperty(propertyName[1]);
                    }
                    if (property != null)
                    {
                        object fieldValue = field.Value;
                        //if (field.Value.GetType().Equals(typeof(String)))
                        //    fieldValue = fieldValue.ToString().Trim();

                        property.SetValue(parentObject, fieldValue, null);
                    }
                }
            }
        }