Exemple #1
0
        public static object ConvertJson2Model(Type type, object model, JsonParamsHelper jsonPH)
        {
            PropertyInfo[] props = type.GetProperties(BindingFlags.Public | BindingFlags.Instance);
            foreach (PropertyInfo p in props)
            {
                string value = jsonPH.GetParamByKey(p.Name);
                if (!string.IsNullOrEmpty(value))
                {
                    switch (p.PropertyType.BaseType.ToString())
                    {
                    case "System.Enum":

                        var enumObj = thmjLib.CreateInstance(p.PropertyType.ToString());
                        if (enumObj != null)
                        {
                            p.SetValue(model, Enum.Parse(thmjLib.CreateInstance(p.PropertyType.ToString()).GetType(), value + string.Empty), new string[] { });
                        }
                        break;

                    case "System.ValueType":
                        switch (p.PropertyType.FullName)
                        {
                        case "System.Int32":
                            p.SetValue(model, value, new string[] { });
                            break;

                        case "System.Guid":
                            p.SetValue(model, Guid.Parse(value), new string[] { });
                            break;

                        case "System.String":
                            p.SetValue(model, value, new string[] { });
                            break;

                        case "System.DateTime":
                            p.SetValue(model, DateTime.Parse(value), new string[] { });
                            break;

                        default:
                            //p.SetValue(model, value, new string[] { });
                            break;
                        }
                        break;

                    default:
                        p.SetValue(model, value, new string[] { });
                        break;
                    }
                }
            }
            return(model);
        }
Exemple #2
0
 public static object ConvertJson2Model(Type type, JsonParamsHelper jsonPH)
 {
     return(ConvertJson2Model(type, Activator.CreateInstance(type), jsonPH));
 }