/// <summary> /// Converte tutte le proprietà dell'oggetto in un Hashtable(nome,valore). /// Attenzione: Vengono convertiti solo i tipi semplici, i tipi che estendono "BaseModel" e le liste vengono ignorati. /// </summary> /// <returns>Hashtable</returns> public Hashtable GetProperties() { Hashtable parameters = new Hashtable(); Type type = this.GetType(); PropertyInfo[] properties = type.GetProperties(); dynamic propertyValue = ""; Type propertyType = null; String propertyName = ""; MethodInfo getter = null; foreach (PropertyInfo property in properties) { propertyType = property.PropertyType; if (propertyType.BaseType != typeof(BaseModel) || propertyType.IsGenericType) // creo solo parametri con tipi semplici { propertyName = property.Name; if (propertyName != "SP_READ" && propertyName != "SP_WRITE") { getter = property.GetGetMethod(); propertyValue = getter.Invoke(this, null); switch (propertyType.ToString()) { case "System.Nullable`1[System.Int32]": propertyValue = Marshaller.ChangeType <int?>(propertyValue); break; case "System.Nullable`1[System.Double]": propertyValue = Marshaller.ChangeType <double?>(propertyValue); break; case "System.Nullable`1[System.Boolean]": propertyValue = Marshaller.ChangeType <bool?>(propertyValue); break; case "System.Nullable`1[System.DateTime]": propertyValue = Marshaller.ChangeType <DateTime?>(propertyValue); break; default: propertyValue = Marshaller.ChangeType <string>(propertyValue); break; } parameters.Add(propertyName, propertyValue); } } } return(parameters); }
/// <summary> /// Copia i dati dal DataRow all'oggetto. /// </summary> /// <param type="DataRow" name="row">DataRow contente i dati del record su database.</param> /// <returns>void</returns> public void FillObject(DataRow row) { Type type = this.GetType(); PropertyInfo property = null; MethodInfo setter = null; Type propertyType = null; dynamic propertyValue = null; DataTable table = row.Table; foreach (DataColumn col in table.Columns) { property = type.GetProperty(col.ColumnName); setter = property.GetSetMethod(); propertyType = property.PropertyType; propertyValue = (row[col.ColumnName] != DBNull.Value) ? row[col.ColumnName] : null; switch (propertyType.ToString()) { case "System.Nullable`1[System.Int32]": propertyValue = Marshaller.ChangeType <int?>(propertyValue); break; case "System.Nullable`1[System.Double]": propertyValue = Marshaller.ChangeType <double?>(propertyValue); break; case "System.Nullable`1[System.Boolean]": propertyValue = Marshaller.ChangeType <bool?>(propertyValue); break; case "System.Nullable`1[System.DateTime]": propertyValue = Marshaller.ChangeType <DateTime?>(propertyValue); break; default: propertyValue = Marshaller.ChangeType <string>(propertyValue); if (propertyValue != null) { string value = (string)propertyValue; propertyValue = value.Trim(); } break; } object[] args = { propertyValue }; setter.Invoke(this, args); } }
public void Search(HttpContext context) { // determino il tipo di oggetto passato string[] typeOf = context.Request["class"].ToString().Split('.'); Type modelType = GetObjectType(context, typeOf[typeOf.Length - 1]); // ricostruisco l'oggetto string data = context.Request["data"]; object modelInstance = Serializer.UnserializeObject(data, modelType, SerializationType.JSON); // ricavo il metodo MethodInfo method = modelType.GetMethod("Search"); // ricavo i parametri di ricerca da passare al metodo string searchdata = context.Request["searchdata"].ToString(); searchdata = searchdata.Replace("{", "").Replace("}", ""); Hashtable searchParameters = new Hashtable(); if (searchdata != "") { string[] searchDataList = searchdata.Split(','); string searchKey = ""; dynamic searchValue = ""; for (int i = 0; i < searchDataList.Length; i++) { searchKey = searchDataList[i].Split(':')[0].Replace("\"", ""); searchValue = searchDataList[i].Split(':')[1].Replace("\"", "").Replace("[", "").Replace("]", ""); if (searchValue == "null") { searchValue = null; } // converto il valore nel tipo definito nell'oggetto PropertyInfo property = modelType.GetProperty(searchKey); Type propertyType = property.PropertyType; switch (propertyType.ToString()) { case "System.Nullable`1[System.Int32]": searchValue = Marshaller.ChangeType <int?>(searchValue); break; case "System.Nullable`1[System.Double]": searchValue = Marshaller.ChangeType <double?>(searchValue); break; case "System.Nullable`1[System.Boolean]": searchValue = Marshaller.ChangeType <bool?>(searchValue); break; case "System.Nullable`1[System.DateTime]": searchValue = Marshaller.ChangeType <DateTime?>(searchValue); break; default: searchValue = Marshaller.ChangeType <string>(searchValue); break; } searchParameters.Add(searchKey, searchValue); } } // invoco il metodo object[] args = { searchParameters }; List <dynamic> modelList = (List <dynamic>)method.Invoke(modelInstance, args); // restituisco al client l'oggetto string output = (modelList != null && modelList.Count > 0) ? Serializer.SerializeObjectList(modelList, SerializationType.JSON) : "{}"; context.Response.Write(output); }