Exemplo n.º 1
0
        /// <summary>
        /// Recursively extracts properties names.
        /// </summary>
        /// <param name="type">Type to extract.</param>
        /// <param name="typePrefix">String prefix of type.</param>
        /// <param name="options">Extraction options.</param>
        /// <returns>List of filed names.</returns>
        public static List <string> ExtractFieldNames(Type type, string typePrefix = null, ExtractionOptions options = null)
        {
            var result = new List <string>();

            PropertyInfo[] properties = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo property in properties)
            {
                if ((options != null) && (options.ExcludeGetIgnored))
                {
                    var ignoreAttributes = Extractor.GetIgnoreAttributes(property);

                    if (ignoreAttributes.Any(x => x.GetType() == typeof(IgnoreForGetAttribute)))
                    {
                        continue; // skip cycle iteration for ignored field
                    }
                }

                if (property.GetCustomAttributes(typeof(ExtractRecursivelyAttribute), true).Length > 0)
                {
                    result.AddRange(Extractor.ExtractFieldNames(property.PropertyType, property.Name, options));
                }
                else
                {
                    if (!String.IsNullOrEmpty(typePrefix))
                    {
                        result.Add(String.Format("{0}.{1}", typePrefix, property.Name));
                    }
                    else
                    {
                        result.Add(property.Name);
                    }
                }
            }

            return(result);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Extracts entity's properties values.
        /// </summary>
        /// <param name="entity">Entity instance.</param>
        /// <param name="options">Extract options.</param>
        /// <returns>Dictionary of property name - property value.</returns>
        public static Dictionary <string, IConvertible> ExtractFieldValues(SalesforceEntityBase entity, ExtractionOptions options = null)
        {
            var result = new Dictionary <string, IConvertible>();

            PropertyInfo[] properties = entity.GetType().GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (PropertyInfo property in properties)
            {
                bool ignoreProperty = false;

                if (options != null)
                {
                    var ignoredAttributes = Extractor.GetIgnoreAttributes(property);

                    if (options.ExcludeGetIgnored)
                    {
                        ignoreProperty = (ignoredAttributes.Any(x => x.GetType() == typeof(IgnoreForGetAttribute)));
                    }
                    else if ((options.ExcludeCreateIgnored) || (options.ExcludeUpdateIgnored))
                    {
                        if (ignoredAttributes.Any(x => x.GetType() == typeof(IgnoreForCreateUpdateAttribute)))
                        {
                            ignoreProperty = true;
                        }
                        else if (options.ExcludeCreateIgnored)
                        {
                            ignoreProperty = (ignoredAttributes.Any(x => x.GetType() == typeof(IgnoreForCreateAttribute)));
                        }
                        else if (options.ExcludeUpdateIgnored)
                        {
                            ignoreProperty = (ignoredAttributes.Any(x => x.GetType() == typeof(IgnoreForUpdateAttribute)));
                        }
                    }
                }

                if (!ignoreProperty)
                {
                    object propertyValue = property.GetValue(entity, null);

                    if (propertyValue != null) // if value is not null
                    {
                        string resultValue = propertyValue.ToString();

                        Type underlyingType = Nullable.GetUnderlyingType(property.PropertyType);

                        if (underlyingType != null) // if has nullable type
                        {
                            if (underlyingType == typeof(DateTime))
                            {
                                resultValue = (propertyValue as DateTime?).Value.ToString("s");
                            }
                        }
                        else // if not nullable type
                        {
                            if (property.PropertyType == typeof(DateTime))
                            {
                                resultValue = (propertyValue as DateTime?).Value.ToString("s");
                            }
                        }

                        result.Add(property.Name, resultValue);
                    }
                    else
                    {
                        result.Add(property.Name, null);
                    }
                }
            }

            return(result);
        }