Esempio n. 1
0
        /// <summary>
        /// Retrieves a Dictionary with name and value
        /// for all object properties matching the given criteria.
        /// </summary>
        private static PropertyContainer ParseProperties <T>(T obj)
        {
            var propertyContainer = new PropertyContainer();

            var typeName      = typeof(T).Name;
            var validKeyNames = new[] { "Id",
                                        string.Format("{0}Id", typeName), string.Format("{0}_Id", typeName) };

            var properties = typeof(T).GetProperties();

            foreach (var property in properties)
            {
                // Skip reference types (but still include string!)
                if (property.PropertyType.IsClass && property.PropertyType != typeof(string))
                {
                    continue;
                }

                // Skip methods without a public setter
                if (property.GetSetMethod() == null)
                {
                    continue;
                }

                // Skip methods specifically ignored
                if (property.IsDefined(typeof(DapperIgnore), false))
                {
                    continue;
                }

                var name  = property.Name;
                var value = typeof(T).GetProperty(property.Name).GetValue(obj, null);

                if (property.IsDefined(typeof(DapperKey), false) || validKeyNames.Contains(name))
                {
                    propertyContainer.AddId(name, value);
                }
                else
                {
                    propertyContainer.AddValue(name, value);
                }
            }

            return(propertyContainer);
        }