Esempio n. 1
0
        public object CreateDataShapedObject(Expense expense, List<string> lstOfFields)
        {
            if (!lstOfFields.Any())
            {
                return expense;
            }
            // create a new ExpandoObject & dynamically create the properties for this object

            var objectToReturn = new ExpandoObject();
            foreach (string field in lstOfFields)
            {
                // need to include public and instance, b/c specifying a binding flag overwrites the
                // already-existing binding flags.

                object fieldValue = expense.GetType()
                    .GetProperty(field, BindingFlags.IgnoreCase | BindingFlags.Public | BindingFlags.Instance)
                    .GetValue(expense, null);

                // add the field to the ExpandoObject
                ((IDictionary<string, object>) objectToReturn).Add(field, fieldValue);
            }

            return objectToReturn;
        }