简体   繁体   中英

Implementing multiple interface using generics

I want to implement the methods in interface using generics. But I am getting error. eg //Filter is a class here.

public interface IComponent<T>
{
  List<T> GetOrderSummary(Filter input);
  T GetOrderDetails(string orderId);
  List<T> GetOrderSummaryDetails(Filter input);
}

// ORDER A AND ORDER B ARE TWO MODEL CLASSES HERE
public class OrderDetails : IComponent<OrderA>,IComponent<OrderB>
{
  public List<OrderA> GetOrderSummary(Filter input)
  {
    //Some logic
    //lst of type OrderA
    return lst;
  }

  public List<OrderB> GetOrderSummaryDetails(Filter input)
  {
    //Some logic
    //lst of type OrderB
    return lst
  }

  public OrderA GetOrderDetails(string orderId)
  {
    throw new NotImplementedException();
  }
}

I am getting error building that

OrderDetails does not implement interface member IComponent.GetOrderSummaryDetails(Filter).OrderDetails.GetOrderSummaryDetails cannot implement IComponent.GetOrderSummaryDetails(Filter) beacuse it does not have the matching return type of List<OrderA>

OrderDetails does not implement interface member IComponent.GetOrderSummary(Filter).OrderDetails.GetOrderSummary cannot implement IComponent.GetOrderSummary(Filter) beacuse it does not have the matching return type of List<OrderB>

OrderDetails does not implement interface member IComponent.GetOrderDetails(string).OrderDetails.GetOrderDetails(string) cannot implement IComponent.GetOrderDetails(string) beacause it does not have the matching return type OrderB

Please let me know how to fix these issues.

You need explicit implementation of both interfaces, like this:

public class OrderDetails : IComponent<OrderA>, IComponent<OrderB>
{
    List<OrderA> IComponent<OrderA>.GetOrderSummary(Filter input)
    {
        throw new NotImplementedException();
    }

    OrderB IComponent<OrderB>.GetOrderDetails(string orderId)
    {
        throw new NotImplementedException();
    }

    List<OrderB> IComponent<OrderB>.GetOrderSummaryDetails(Filter input)
    {
        throw new NotImplementedException();
    }

    List<OrderB> IComponent<OrderB>.GetOrderSummary(Filter input)
    {
        throw new NotImplementedException();
    }

    OrderA IComponent<OrderA>.GetOrderDetails(string orderId)
    {
        throw new NotImplementedException();
    }

    List<OrderA> IComponent<OrderA>.GetOrderSummaryDetails(Filter input)
    {
        throw new NotImplementedException();
    }
}

Later, to access proper method you can use either casting or proper variable type:

var orderDetails1 = new OrderDetails();
var details1 = ((IComponent<OrderA>)orderDetails1).GetOrderDetails("");

IComponent<OrderA> orderDetails2 = new OrderDetails();
var details2 = orderDetails2.GetOrderDetails("");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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