如何将此方法转换为通用形式。由于对象的属性,我无法这样做。
private async Task<T> GetSalesforceMachineData<T>()
{
T machineData = await CallSalesforceApi<T>();
while (!machineData.done)
{
var encodedUrl = HttpUtility.UrlEncode(machineData.nextRecordsUrl);
T nextData = await GetNextData<T>(encodedUrl);
machineData.done = nextData.done; //not able to convert because of these properties
machineData.nextRecordsUrl = nextData.nextRecordsUrl;//not able to convert because of these properties
machineData.records.AddRange(nextData.records);//not able to convert because of these properties
}
return machineData;
}
Run Code Online (Sandbox Code Playgroud)
像这样的东西
private async Task<T> GetSalesforceMachineData<T>()
{
T machineData = await CallSalesforceApi<T>();
while (!machineData.done)
{
var encodedUrl = HttpUtility.UrlEncode(machineData.nextRecordsUrl);
T nextData = await GetNextData<T>(encodedUrl);
machineData.done = nextData.done;
machineData.nextRecordsUrl = nextData.nextRecordsUrl;
machineData.records.AddRange(nextData.records);
}
return machineData;
}
Run Code Online (Sandbox Code Playgroud)
泛型与模板不同 - 编译器需要能够静态解析您正在使用的所有可能的T、已知的和未知的成员。实现此目的的一种方法是定义一个具有这些成员的接口(或基类型),使所有必需的类型实现该接口(这通常应该像添加一样简单: IYourNewInterface,因为成员可能已经存在于公共 API 上,假设它们不是裸露字段),并添加where T : IYourNewInterface到通用方法中。
| 归档时间: |
|
| 查看次数: |
64 次 |
| 最近记录: |