【发布时间】:2019-05-01 07:27:52
【问题描述】:
public void ResponseHandler<T>( string responseContent, ref Result<T> result)
where T : IServiceModel
{
var respModel = responseContent.FromJson<OrderResponse>();
if (respModel.Status.Equals(_innerConfig.SuccessTradeStatus, StringComparison.OrdinalIgnoreCase))
{
result.IsSuccess = true;
result.Data.TradeNo = respModel.Transaction_id;// CAN NOT resolve symbol TradeNo
}
...
}
public class Result<T> : Result
{
public T Data { get; set; }
}
public class MyModel:IServiceModel
{
public string TradeNo { get; set; }
}
public interface IServiceModel
{
}
用法:ServiceProvider.ResponseHandler<MyModel>(responseContent, ref result);
问题是我无法获取属性TradeNo,
我找到了另一个线程:Generic functions and ref returns in C# 7.0
但不确定我的代码是否存在同样的问题。
有什么建议吗?谢谢。 :)
【问题讨论】:
-
欢迎来到 Stack Overflow。
TradeNo在哪里声明?您能否提供一个minimal reproducible example,以便我们可以自己重现问题,以及所有相关类型? (您确定这与ref参数有关吗?不清楚为什么这是ref参数,因为您没有更改result变量本身的值。) -
您无法访问 TradeNo 属性,因为
ResponseHandler<T>不知道它正在处理的类型 -
@NitinSawant 任何铸造方法?
-
MyModel没有在您显示的代码中实现IServiceModel,因此您甚至无法使用Result<MyModel>调用ResponseHandler。即使它确实实现了ISerivceModel,您的方法也声称能够处理anyIServiceModel类型的结果。也许它应该是非通用的?public void ResponseHandler(string responseContent, Result<MyModel> model)?
标签: c# generics pass-by-reference c#-7.0