#region Assign parameters to specified objects /// <summary> /// Assign parameters to specified objects /// </summary> /// <typeparam name="T">object type</typeparam> /// <param name="dic">Fields/values</param> /// <returns></returns> static public T Assign<T>(Dictionary<string, string> dic) where T : new() { Type myType = typeof(T); T entity = new T(); var fields = myType.GetProperties(); string val = string.Empty; object obj = null; foreach (var field in fields) { if (!dic.ContainsKey(field.Name)) continue; val = dic[field.Name]; object defaultVal; if (field.PropertyType.Name.Equals("String")) defaultVal = ""; else if (field.PropertyType.Name.Equals("Boolean")) { defaultVal = false; val = (val.Equals("1") || val.Equals("on")).ToString(); } else if (field.PropertyType.Name.Equals("Decimal")) defaultVal = 0M; else defaultVal = 0; if (!field.PropertyType.IsGenericType) obj = string.IsNullOrEmpty(val) ? defaultVal : Convert.ChangeType(val, field.PropertyType); else { Type genericTypeDefinition = field.PropertyType.GetGenericTypeDefinition(); if (genericTypeDefinition == typeof(Nullable<>)) obj = string.IsNullOrEmpty(val) ? defaultVal : Convert.ChangeType(val, Nullable.GetUnderlyingType(field.PropertyType)); } field.SetValue(entity, obj, null); } return entity; } #endregion #region Converting parameter strings to dictionary arrays /// <summary> /// Converting parameter strings to dictionary arrays /// </summary> static public Dictionary<String, String> GetFormData(string formData) { try { String[] dataArry = formData.Split('&'); Dictionary<String, String> dataDic = new Dictionary<string, string>(); for (int i = 0; i <= dataArry.Length - 1; i++) { String dataParm = dataArry[i]; int dIndex = dataParm.IndexOf("="); String key = dataParm.Substring(0, dIndex); String value = dataParm.Substring(dIndex + 1, dataParm.Length - dIndex - 1); dataDic.Add(key, value); } return dataDic; } catch (Exception ex) { throw new Exception(ex.Message); } } #endregion