Exemple #1
0
        public void empty_dictionary_returns_empty_dictionary()
        {
            var dictionary = Empty.Dictionary <int, string>();

            dictionary.ShouldNotBeNull();
            dictionary.ShouldBeEmpty();
        }
    public static void Demo()
    {
        Method_1(Empty.Collection());
        Method_2(Empty.Collection <string>());
        Method_3(Empty.Collection <int>());
        Method_4(Empty.Collection <int>());

        Method_5(Empty.Dictionary <string, int>());
    }
Exemple #3
0
        /// <summary>
        /// Gets all properties values from type. Returns dictionary in the form of property name - value. If source object is null returns empty dictionary
        /// </summary>
        /// <param name="type">The source type</param>
        /// <param name="source">The source object to get properties values from</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Source type is null</exception>
        public static Dictionary <string, object> GetPropertiesValues(this Type type, object source)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (source == null)
            {
                return(Empty.Dictionary <string, object>());
            }

            return(type
                   .GetProperties()
                   .ToDictionary(x => x.Name, x => x.GetValue(source, null)));
        }
Exemple #4
0
        /// <summary>
        /// Gets all constants, fields and properties values from type. Returns dictionary in the form of field name - value. If source object is null returns empty dictionary
        /// </summary>
        /// <param name="type">The source type</param>
        /// <param name="source">The source object to get fields values from</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Source type is null</exception>
        public static Dictionary <string, object> GetAllObjectData(this Type type, object source)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (source == null)
            {
                return(Empty.Dictionary <string, object>());
            }

            return(type
                   .GetConstantValues()
                   .Concat(type.GetFieldsValues(source))
                   .Concat(type.GetPropertiesValues(source)));
        }
Exemple #5
0
        /// <summary>
        /// Gets all fields values from type. Returns dictionary in the form of field name - value. If source object is null returns empty dictionary
        /// </summary>
        /// <param name="type">The source type</param>
        /// <param name="source">The source object to get fields values from</param>
        /// <returns></returns>
        /// <exception cref="ArgumentNullException">Source type is null</exception>
        public static Dictionary <string, object> GetFieldsValues(this Type type, object source)
        {
            if (type == null)
            {
                throw new ArgumentNullException(nameof(type));
            }

            if (source == null)
            {
                return(Empty.Dictionary <string, object>());
            }

            return(type
                   .GetFields(BindingFlags.NonPublic | BindingFlags.Public | BindingFlags.Instance)
                   .Where(x => !x.Name.EndsWith(BackingFieldSuffix))
                   .ToDictionary(x => x.Name, x => x.GetValue(source)));
        }