Exemple #1
0
        public static void FillNullProperties(object source, Func <PropertyInfo, object> propertyValueFactory)
        {
            Precondition.ArgumentNotNull(source, nameof(source));
            Precondition.ArgumentNotNull(propertyValueFactory, nameof(propertyValueFactory));

            var sourceType = source.GetType();

            var propertyInfos = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var propertyInfo in propertyInfos)
            {
                var propertyType  = propertyInfo.PropertyType;
                var setMethodInfo = propertyInfo.GetSetMethod();
                if (!propertyType.IsClass || setMethodInfo == null || !setMethodInfo.IsPublic || !propertyInfo.CanWrite)
                {
                    continue;
                }

                var propertyValue = propertyInfo.GetValue(source);
                if (propertyValue != null)
                {
                    FillNullProperties(propertyValue, propertyValueFactory);
                }
                else if (propertyInfo.CanWrite)
                {
                    propertyValue = propertyValueFactory(propertyInfo);
                    if (propertyValue == null)
                    {
                        continue;
                    }
                    FillNullProperties(propertyValue, propertyValueFactory);
                    propertyInfo.SetValue(source, propertyValue);
                }
            }
        }
Exemple #2
0
        public static void IterateProperties(object source, Func <PropertyInfo, object, object> callback)
        {
            Precondition.ArgumentNotNull(source, nameof(source));
            Precondition.ArgumentNotNull(callback, nameof(callback));

            var sourceType = source.GetType();

            var propertyInfos = sourceType.GetProperties(BindingFlags.Public | BindingFlags.Instance);

            foreach (var propertyInfo in propertyInfos)
            {
                var setMethodInfo = propertyInfo.GetSetMethod();
                if (setMethodInfo == null || !setMethodInfo.IsPublic || !propertyInfo.CanWrite)
                {
                    continue;
                }
                var propertyValue = propertyInfo.GetValue(source);
                propertyValue = callback(propertyInfo, propertyValue);
                propertyInfo.SetValue(source, propertyValue);
                if (propertyValue != null)
                {
                    IterateProperties(propertyValue, callback);
                }
            }
        }
Exemple #3
0
        public static string ToVariableName(string st)
        {
            Precondition.ArgumentNotNull(st, nameof(st));
            var chars = st.ToCharArray();

            if (chars.Length > 0)
            {
                chars[0] = char.ToLower(chars[0]);
            }
            return(new string(chars));
        }
Exemple #4
0
 public static object CreateDefaultInstance(Type fromType)
 {
     Precondition.ArgumentNotNull(fromType, nameof(fromType));
     if (DefaultTypeInstances.ContainsKey(fromType))
     {
         return(DefaultTypeInstances[fromType]);
     }
     return(fromType.GetConstructors().Any(constructor => constructor.GetParameters().Length == 0)
         ? Activator.CreateInstance(fromType)
         : null);
 }
Exemple #5
0
        public static object ExecuteMethod(object source, string methodName, params object[] paramsObjects)
        {
            Precondition.ArgumentNotNull(source, nameof(source));
            Precondition.ArgumentNotNull(methodName, nameof(methodName));

            return(source.GetType().GetMethod(methodName)?.Invoke(
                       source,
                       BindingFlags.Instance,
                       null,
                       paramsObjects,
                       CultureInfo.CurrentCulture));
        }
Exemple #6
0
 /// <summary>
 /// https://stackoverflow.com/questions/323314/best-way-to-convert-pascal-case-to-a-sentence
 /// </summary>
 public static string ToFriendlyString(string st)
 {
     Precondition.ArgumentNotNull(st, nameof(st));
     return(Regex.Replace(st, "[a-z][A-Z]", m => $"{m.Value[0]} {char.ToLower(m.Value[1])}"));
 }