简体   繁体   English

c#使用运行时已知类型的通用方法进行异步方法

[英]c# using a generic method with a type known at runtime for asynchronous method

I'm trying to do a rest api call where I want to read into an object known at runtime. 我试图做一个REST API调用,我想读入运行时已知的对象。 Code is simplified for example. 例如,代码被简化。

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() isn't the right approach. o.GetType()是不正确的方法。 Is there something I can do that can determine the correct generic type at runtime for this? 我有什么可以在运行时为此确定正确的泛型类型的吗?

o = await response.Content.ReadAsAsync(o.GetType())

请参见HttpContentExtensions.ReadAsAsync方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM