重写通用用法类 - C#MonoTouch

时间:2014-04-08 08:17:43

标签: c# xamarin.ios xamarin

我正在使用Xamarin.iOS / MonoTouch处理iOS应用程序,而且我遇到了一个两难的困境。我们通过查询JSON文件来下载相当多的数据,然后将这些文件处理成保存在本地sqlite数据库中的模型。问题是我编写的类是针对特定类型编写的,我希望能够使用相同的类将所有JSON数据提取到本地对象中。

这是我的代码:

using System;
using System.IO;
using System.Net;
using Newtonsoft.Json;
using System.Collections.Generic;

#pragma warning disable 0414 // Supressing Warning CS0414:

namespace CommonLibrary {
    public class JSONHandler {

        // Debug Constants:
        private static String DEBUG_FILE_TAG = "[JSONHandler] ";

        // Define variables:
        private Uri JSONRequestURL;
        private bool RequestTimedOut;
        private bool RequestSuccessful;
        private string ResponseContent;
        private List<Post> JSONObjects;

        // Define objects:
        private HttpWebRequest JSONWebRequest;
        private HttpWebResponse JSONWebResponse;


        // Constructor:
        public JSONHandler(string requestURL){

            // Set request URL:
            this.JSONRequestURL = new Uri(requestURL);

            // Set default statuses:
            this.RequestTimedOut = false;
            this.RequestSuccessful = false;

        }


        // Create web request:
        private void CreateWebRequest(){
            this.JSONWebRequest = (HttpWebRequest) WebRequest.Create (this.JSONRequestURL);
            this.JSONWebRequest.Method = "GET";
            this.JSONWebRequest.Timeout = 5000;
            this.JSONWebRequest.KeepAlive = false;
            this.JSONWebRequest.AllowAutoRedirect = false;
            this.JSONWebRequest.ContentType = "application/json";
        }


        // Get request response:
        private void GetRequestResponse(){
            try {

                // Catch the response:
                this.JSONWebResponse = (HttpWebResponse) this.JSONWebRequest.GetResponse ();

                // Check the status code:
                if (this.JSONWebResponse.StatusCode == HttpStatusCode.OK){

                    // Get content:
                    StreamReader reader = new StreamReader (this.JSONWebResponse.GetResponseStream ());
                    this.ResponseContent = reader.ReadToEnd();

                    // Close response:
                    this.JSONWebResponse.Close();

                    // Check response length:
                    if (!String.IsNullOrWhiteSpace(this.ResponseContent)){
                        this.JSONObjects = JsonConvert.DeserializeObject<List<Post>>(this.ResponseContent);
                        this.RequestSuccessful = true;
                    } else {
                        this.RequestSuccessful = false;
                    }

                } else {
                    this.RequestSuccessful = false;
                }

            } catch (WebException){
                this.RequestTimedOut = true;
                this.RequestSuccessful = false;
            } catch (TimeoutException){
                this.RequestTimedOut = true;
                this.RequestSuccessful = false;
            }
        }


        // Fetch JSON from server:
        public void FetchJSON(){
            this.CreateWebRequest ();
            this.GetRequestResponse ();
        }


        // Return request status:
        public bool RequestWasSuccessful(){
            return RequestSuccessful;
        }


        // Return timeout status:
        public bool RequestDidTimeOut(){
            return RequestTimedOut;
        }


        // Get object count:
        public int GetJSONCount(){
            return this.JSONObjects.Count;
        }


        // Get list of objects:
        public List<Post> GetJSONObjects (){
            return this.JSONObjects;
        }


    }
}

如您所见,我必须将列表中存储的类型从Post更改为任何其他对象并创建新文件,例如JSONPost,JSONRunner,JSONLayer等,我想要只使用一个类JSONHandler来处理它。希望有人可以帮助我解决这个问题。我现在要上下课:

  • RelayTeam
  • RelayRunner
  • RelayRunnerResult
  • MarathonRunner
  • MarathonRunnerResult

正如大家都能理解的那样,只有所有这些文件都有重复文件才能获得好处。

我非常感谢能得到任何帮助!

祝你好运, 乔纳森

1 个答案:

答案 0 :(得分:5)

使用泛型 - 如果JSONObjects集合的类型是唯一不同的,你可以这样做

public class JSONHandler<T> {
  ...
  private List<T> JSONObjects;

创建新的JSONHandler实例时,可以指定类型

var handler = new JSONHandler<Post>();
var handler = new JSONHandler<Layer>();
var handler = new JSONHandler<RelayTeam>();