C# 我们能让这个方法通用吗?

C# 我们能让这个方法通用吗?,c#,json,generics,xamarin,C#,Json,Generics,Xamarin,我在一个示例中看到了这个方法,使用JSON访问REST服务器: List<Country> countries = new List<Country>(); public Task<List<Country>> GetCountries() { return Task.Factory.StartNew (() => { try { if(countrie

我在一个示例中看到了这个方法,使用JSON访问REST服务器:

List<Country> countries = new List<Country>();
    public Task<List<Country>> GetCountries()
    {
        return Task.Factory.StartNew (() => {
            try {

                if(countries.Count > 0)
                    return countries;

                var request = CreateRequest ("Countries");
                string response = ReadResponseText (request);
                countries = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Country>> (response);
                return countries;
            } catch (Exception ex) {
                Console.WriteLine (ex);
                return new List<Country> ();
            }
        });
    }
List countries=新列表();
公共任务(国家)
{
返回Task.Factory.StartNew(()=>{
试一试{
如果(countries.Count>0)
返回国;
var请求=CreateRequest(“国家”);
字符串响应=ReadResponseText(请求);
countries=Newtonsoft.Json.JsonConvert.DeserializeObject(响应);
返回国;
}捕获(例外情况除外){
Console.WriteLine(ex);
返回新列表();
}
});
}
其中,“CreateRequest”和“ReadResponseText”是与REST服务器交互的方法,基本上接收要反序列化的国家列表并在列表中返回。 现在,我尝试将此方法设置为泛型,以便接收类型并返回指定类型的对象的泛型列表,如下所示:

public static Task<List<Object>> getListOfAnyObject(string requested_object, Type type) 
    {
        return Task.Factory.StartNew (() => {
            try {
                var request = CreateRequest (requested_object);
                string response = ReadResponseText (request);
                List<Object> objects = // create a generic list based on the specified type
                objects = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Object>> (response); // not sure how to handle this line
                return objects;
            } catch (Exception ex) {
                Console.WriteLine (ex);
                return ex.Message;
            }
        });
    }
公共静态任务getListOfAnyObject(请求的字符串\u对象,类型)
{
返回Task.Factory.StartNew(()=>{
试一试{
var request=CreateRequest(请求的对象);
字符串响应=ReadResponseText(请求);
List objects=//基于指定的类型创建一个通用列表
objects=Newtonsoft.Json.JsonConvert.DeserializeObject(响应);//不确定如何处理此行
归还物品;
}捕获(例外情况除外){
Console.WriteLine(ex);
返回ex.消息;
}
});
}
所以我的问题是,我如何创建上面的方法,以便越来越少地像这样使用它(将列表转换为我想要的类型)

List countries=(List)(List)getListofAnoyObject(“countries”,Country.type);

非常感谢

试试这样的

public static Task<List<T>> getListOfAnyObject<T>(string requested_object) 
{
   return Task.Factory.StartNew (() => {
       try {
           var request = CreateRequest (requested_object);
           string response = ReadResponseText (request);
           return Newtonsoft.Json.JsonConvert.DeserializeObject<List<T>> (response); // not sure how to handle this line
       } catch (Exception ex) {
           Console.WriteLine (ex);
           return ex.Message;
       }
   });
}
公共静态任务getListOfAnyObject(请求的字符串\u对象)
{
返回Task.Factory.StartNew(()=>{
试一试{
var request=CreateRequest(请求的对象);
字符串响应=ReadResponseText(请求);
返回Newtonsoft.Json.JsonConvert.DeserializeObject(响应);//不确定如何处理此行
}捕获(例外情况除外){
Console.WriteLine(ex);
返回ex.消息;
}
});
}
像这样叫

List<Country> countries = getListOfAnyObject<Country>("countries");
List countries=getListofAnoyObject(“国家”);

Newtonsoft为反序列化提供了一种异步方法(
DeserializeObjectAsync
)。您好!你的方法非常有效,非常感谢!我只是将catch返回调整为这样,否则编译器会抱怨说整个代码没有返回值:(…)}catch(Exception ex){Console.WriteLine(ex);return new List();});}考虑将函数名更改为类似于
getAll(“country”)
。。。根据您的需要,您还可以使用通用存储库模式,因此只需说
repo.GetAll()它知道你在说什么
List<Country> countries = getListOfAnyObject<Country>("countries");