我有以下代码来获取星球大战API对象的列表:
public static async Task<IList<T>> Get<T>()
where T : SWAPIEntity
{
var entities = new List<T>();
var rootUrl = (string)typeof(T).GetProperty("rootUrl").GetValue(null);
var url = $"{baseUrl}/{rootUrl}";
var result = await GetResult<T>(url);
entities.AddRange(result.results);
while (result.next != null)
{
result = await GetResult<T>(result.next);
entities.AddRange(result.results);
}
return entities;
}
我想去哪里
rootUrl
取决于哪种类型的
SWAPIEntity
被传为
T
.
上面的代码抛出
“非静态方法需要一个目标。”
虚荣
:
public class SWAPIEntity
{
public string name { get; }
}
虚荣
公营部门
{
公共字符串名称{get;}
Planet
:
public class Planet : SWAPIEntity
{
public string rootUrl { get; } = "planets";
public string climate { get; set; }
}
我称之为
await StarWarsApi.Get<Planet>();
根URL
取决于哪种类型的
虚荣
我在试着得到什么?