重写通用用法类 - C#MonoTouch

Jon*_*han 0 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;
        }


    }
}
Run Code Online (Sandbox Code Playgroud)

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

  • 岗位
  • RelayTeam
  • RelayRunner
  • RelayRunnerResult
  • MarathonRunner
  • MarathonRunnerResult

正如大家都能理解的那样,为所有这些文件提供重复文件并不好.

我真的很感谢能得到的任何帮助!

最好的问候,乔纳森

Jas*_*son 5

使用泛型 - 如果JSONObjects集合的类型是唯一不同的,则可以执行此操作

public class JSONHandler<T> {
  ...
  private List<T> JSONObjects;
Run Code Online (Sandbox Code Playgroud)

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

var handler = new JSONHandler<Post>();
var handler = new JSONHandler<Layer>();
var handler = new JSONHandler<RelayTeam>();
Run Code Online (Sandbox Code Playgroud)