Reputation: 455
I want to implement the methods in interface using generics. But I am getting error. e.g. //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.
Upvotes: 0
Views: 56
Reputation: 3329
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("");
Upvotes: 1