Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/ms-access/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 多形(?)异步方法_C#_Polymorphism - Fatal编程技术网

C# 多形(?)异步方法

C# 多形(?)异步方法,c#,polymorphism,C#,Polymorphism,我的ASP.Net MVC控制器中有以下代码: public ActionResult GetAnimals() { List<Dog> dogs = GetDogs("api/dogs).Result; List<Cat> dogs = GetCats("api/cats).Result; List<Horse> dogs = GetCats("api/cats).Result; ... ... } private async T

我的ASP.Net MVC控制器中有以下代码:

public ActionResult GetAnimals()
{
    List<Dog> dogs = GetDogs("api/dogs).Result;
    List<Cat> dogs = GetCats("api/cats).Result;
    List<Horse> dogs = GetCats("api/cats).Result;
...
...
}


private async Task<List<Dog>> GetDogs(string requestUri)
{
    String ret = "";
    var client = new HttpClient();
    client.BaseAddress = new Uri("http://theservice.com/");
    var response = client.GetAsync(requestUri).Result;

    if (response.IsSuccessStatusCode)
    {
        ret = await response.Content.ReadAsAsync<List<Dog>>();
    }
    else
    { }
    return ret;
}

private async Task<List<Cat>> GetCats(string requestUri)
{
    String ret = "";
    var client = new HttpClient();

    client.BaseAddress = new Uri("http://theservice.com/");
    var response = client.GetAsync(requestUri).Result;

    if (response.IsSuccessStatusCode)
    {
        ret = await response.Content.ReadAsAsync<List<Cat>>();
    }
    else
    { }
    return ret;
}
public ActionResult GetAnimals()
{
列出狗=狗(“api/狗”)。结果;
列出狗=猫(“api/cats”)。结果;
列出狗=猫(“api/cats”)。结果;
...
...
}
专用异步任务GetDogs(字符串请求URI)
{
字符串ret=“”;
var client=新的HttpClient();
client.BaseAddress=新Uri(“http://theservice.com/");
var response=client.GetAsync(requestUri).Result;
if(响应。IsSuccessStatusCode)
{
ret=wait response.Content.ReadAsAsync();
}
其他的
{ }
返回ret;
}
私有异步任务GetCats(字符串requestUri)
{
字符串ret=“”;
var client=新的HttpClient();
client.BaseAddress=新Uri(“http://theservice.com/");
var response=client.GetAsync(requestUri).Result;
if(响应。IsSuccessStatusCode)
{
ret=wait response.Content.ReadAsAsync();
}
其他的
{ }
返回ret;
}
根据返回类型,如何避免反复编写Get方法。必须有一种聪明的方法!

使用泛型:

private async Task<List<T>> GetItems<T>(string requestUri)
{
    String ret = "";
    var client = new HttpClient();

    client.BaseAddress = new Uri("http://theservice.com/");
    var response = await client.GetAsync(requestUri);

    if (response.IsSuccessStatusCode)
    {
        ret = await response.Content.ReadAsAsync<List<T>>();
    }
    else
    { }
    return ret;
}

或者直接作为
GetItems(requestUri)

作为旁注,尽量避免直接访问任务的.Result属性。这是一个阻塞操作。相反,也可以对client.GetAsync方法使用wait。
private async Task<List<Dog>> GetDogs(string requestUri)
{
    return GetItems<Dog>(requestUri);
}