0
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
  {
  }

usage : ServiceProvider.ResponseHandler<MyModel>(responseContent, ref result);

The problem is I can not get the property TradeNo, I found another thread: Generic functions and ref returns in C# 7.0 But not sure it is the same problem with my code. Any suggestions?Thanks. :)

YUu
  • 69
  • 8
  • 2
    Welcome to Stack Overflow. Where is `TradeNo` declared? Could you provide a [mcve] so we can reproduce the problem ourselves, with all the relevant types? (And are you sure this is related to `ref` parameters? It's not clear why this is a `ref` parameter at all, as you're not changing the value of the `result` variable itself.) – Jon Skeet Nov 29 '18 at 09:23
  • you cannot access the TradeNo property because the `ResponseHandler` doesn't know about the type it is handling – Nitin Sawant Nov 29 '18 at 09:27
  • @NitinSawant any casting approach? – YUu Nov 29 '18 at 09:28
  • 1
    `MyModel` doesn't implement `IServiceModel` in the code you've shown, so you couldn't even call `ResponseHandler` with a `Result`. Even if it *did* implement `ISerivceModel`, your method claims to be able to handle a result for *any* `IServiceModel` type. Perhaps it should be non-generic? `public void ResponseHandler(string responseContent, Result model)`? – Jon Skeet Nov 29 '18 at 09:29

1 Answers1

3

This is because Data is of type T

public class Result<T> : Result
{
    public T Data { get; set; }
}

and only constraint for T is where T : IServiceModel

If you need to access TradeNo you need to limit T to MyModel or add this property to IServiceModel

public interface IServiceModel
{
    string TradeNo  { get; set; }
}
Pablo notPicasso
  • 3,031
  • 3
  • 17
  • 22