Пример #1
0
        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);
        }