Ejemplo n.º 1
0
        /// <summary>
        /// Combines the property into a list
        /// new(\"First\" as field__First, \"Last\" as field__Last) ==> Dictionary[string, string]
        /// </summary>
        /// <param name="self">The self.</param>
        /// <param name="propertyName">Name of the property.</param>
        /// <returns></returns>
        public static IEnumerable<DataItem> GetDataItems(this DynamicClass self, string propertyName)
        {
            var propertyType = self.GetType();
            var propertyInfo = propertyType.GetProperty(propertyName);

            if (propertyInfo == null)
            {
                return new List<DataItem>();
            }

            var property = propertyInfo.GetValue(self, null);
            var props = property.GetType().GetProperties().Where(p => p.Name.Contains("__"));

            return props
                // Split on __ to get the prefix and the field
                .Select(prop => new { PropertyInfo = prop, Data = prop.Name.Split(new[] { "__" }, StringSplitOptions.None) })

                // Return the Fieldname, Prefix and the the value ('First' , 'field' , 'First')
                .Select(x => new DataItem { Fieldname = x.Data.Last(), Prefix = x.Data.First(), Value = x.PropertyInfo.GetValue(property, null) })
            ;
        }