Thursday, 9 June 2016

Reflection to fill class from another class through Request.Form

No comments
Relection 


 public T ParseObject<T>() where T : new()
        {
            var classObj = new T();
            for (int iForm = 0; iForm < Request.Form.Count; iForm++)
            {
                foreach (PropertyInfo info in classObj.GetType().GetProperties())
                {                
                        PropertyInfo propertyInfos = classObj.GetType().GetProperty(info.Name);
                        Type propertyType = info.PropertyType;
                        if (propertyInfos.PropertyType == typeof(Nullable<Int32>))
                        {
                            info.SetValue(classObj, (ReflectionConversion.ConvertToInt(Request.Form[iForm])), null);
                        }
                        else if (propertyInfos.PropertyType == typeof(Int32))
                        {
                            info.SetValue(classObj, (ReflectionConversion.ConvertToInt(Request.Form[iForm])), null);
                        }
                        else {
                            info.SetValue(classObj, Request.Form[iForm], null);
                        }                  
                }
            }
            return classObj;

        }

Consuming Reflector in controller

var personalDetails = ParseObject<PersonalDetails>();

Validating (conversion) Reflector Data


 public static class ReflectionConversion
    {
        public static string ConvertToDateString(object date)
        {
            if (date == null)
                return string.Empty;

            return date == null ? string.Empty : Convert.ToDateTime(date).ConvertDate();
        }

        public static string ConvertToString(object value)
        {
            return Convert.ToString(ReturnEmptyIfNull(value));
        }

        public static int ConvertToInt(object value)
        {
            return Convert.ToInt32(ReturnZeroIfNull(value));
        }

        public static long ConvertToLong(object value)
        {
            return Convert.ToInt64(ReturnZeroIfNull(value));
        }

        public static decimal ConvertToDecimal(object value)
        {
            return Convert.ToDecimal(ReturnZeroIfNull(value));
        }

        public static DateTime convertToDateTime(object date)
        {
            return Convert.ToDateTime(ReturnDateTimeMinIfNull(date));
        }

        public static string ConvertDate(this DateTime datetTime, bool excludeHoursAndMinutes = false)
        {
            if (datetTime != DateTime.MinValue)
            {
                if (excludeHoursAndMinutes)
                    return datetTime.ToString("yyyy-MM-dd");
                return datetTime.ToString("yyyy-MM-dd HH:mm:ss.fff");
            }
            return null;
        }
        public static object ReturnEmptyIfNull(this object value)
        {
            if (value == DBNull.Value)
                return string.Empty;
            if (value == null)
                return string.Empty;
            return value;
        }
        public static object ReturnZeroIfNull(this object value)
        {
            if (value == DBNull.Value)
                return 0;
            if (value == null)
                return 0;
            return value;
        }
        public static object ReturnDateTimeMinIfNull(this object value)
        {
            if (value == DBNull.Value)
                return DateTime.MinValue;
            if (value == null)
                return DateTime.MinValue;
            return value;
        }

    }