c#使用泛型方法,在运行时已知类型的异步方法

问题描述 投票:0回答:1

我正在尝试做一个休息api调用,我想读入运行时已知的对象。例如,简化了代码。

object o = null;
if (True)  //condition irrelevant just to indicate selection control
    o = new List<Product>();
else
    o = new Product();

 HttpResponseMessage response = await client.GetAsync(path);
 o = await response.Content.ReadAsAsync<o.GetType()>();

o.GetType()不是正确的方法。我能做些什么可以在运行时为此确定正确的泛型类型?

c# rest generics async-await runtime
1个回答
1
投票
o = await response.Content.ReadAsAsync(o.GetType())

HttpContentExtensions.ReadAsAsync Method

© www.soinside.com 2019 - 2024. All rights reserved.