Showing posts with label reflection. Show all posts
Showing posts with label reflection. Show all posts

Thursday, 21 July 2016

Pass and Return different class in generic class for Reflection

No comments
Generic (Reflection) Method

 public System.Collections.Generic.List<T> GetValues<T>(object t) where T : class
        {          
            System.Collections.Generic.List<T> lstUserAppconfig = new System.Collections.Generic.List<T>();
            foreach (PropertyInfo info in t.GetType().GetProperties())
            {
                if (Enum.IsDefined(typeof(Constant.Menus), info.Name))//check if value is in enum
                {
                    if (!string.IsNullOrEmpty((string)info.GetValue(t, null)))
                    {
                        UserAppconfig userAppconfig = new UserAppconfig();
                        userAppconfig.appconfigid = (int)Enum.Parse(typeof(Constant.Menus), info.Name);
                        userAppconfig.userid = SessionFactory.Instance.CurrentUsers.userid;
                        userAppconfig.value = (string)info.GetValue(t, null);
                        lstUserAppconfig.Add((T)Convert.ChangeType(userAppconfig, typeof(T)));
                    }
                }
            }
            return lstUserAppconfig;

        }

Calling method

UserAppconfigModel userAppconfigModel=new UserAppconfigModel()

System.Collections.Generic.List<UserAppconfig> lstUserAppconfig=GetValues<UserAppconfig>(userAppconfigModel);

read more

Friday, 17 June 2016

Multiple if else statement - short by reflection

No comments
Multiple if-else statement

bool chkStatus = true;
            if (DetailsData != null)
            {
                if (DetailsData.Approved != null)
                {
                    if (DetailsData.Approved == false)
                    {
                        chkStatus = false;
                    }
                }
            }
            else
            {
                chkStatus = false;
            }

            if (AspirationData != null)
            {
                if (AspirationData.Approved != null)
                {
                    if (AspirationData.Approved == false)
                    {
                        chkStatus = false;
                    }
                }
            }
            else
            {
                chkStatus = false;
            }


//Short this if else by reflection

 private bool IsWizardApproved<T>(T t)
        {
            if (t == null) return false;
            foreach (PropertyInfo info in t.GetType().GetProperties())
            {
                if (info.Name.ToLower() == "approved")
                {
                    PropertyInfo propertyInfos = t.GetType().GetProperty(info.Name);
                    if (info.GetValue(t, null) != null)
                        return (bool)info.GetValue(t, null);
                }
            }
            return false;

        }

//Call IsWizardApproved for class

   model.Approved = (IsWizardApproved<DetailsData>(detailsData) &&
                && IsWizardApproved<AspirationData>(aspirationData);
read more

Fill dictionary with Table (model) column name and its value - with reflection

No comments
//Dictionary

 public Dictionary<string, string> BindModelDictioanry<T>(T t) where T : class
        {
            Dictionary<string, string> dictModel = new Dictionary<string, string>();
            foreach (PropertyInfo info in t.GetType().GetProperties())
            {
                PropertyInfo propertyInfos = t.GetType().GetProperty(info.Name);
                if (propertyInfos.PropertyType == typeof(ListItemValue))
                {
                    var value = (your column datatype)info.GetValue(t, null);
                    var key = info.Name; //This will be column name
                    dictModel.Add(key, value.ToString()); //This will be column value
                }          
            }
            return dictModel;

        }

//Using reflection

ViewModel model = new ViewModel();
model.DicFormaData = BindModelDictioanry<PersonalDetails>(personalDetailsData);

//model.DicFormaData 

public Dictionary<string, string> DicFormaData { get; set; }

read more

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;
        }

    }


read more