Extension methods for two different classes

爷,独闯天下 提交于 2021-01-07 01:14:27

问题


I have a situation where in C# I consume data via an OData API. Actually, there are two different OData endpoints (two different models), where one is a superset of the other.

I have created two different C# projects (A and B) which contain code to interact with the two API's. The code is automatically generated using Unchase OData Connected Service extension. As a result, project A and project B both contain a class ODataService, which is the main type to interact with. Depending on whether I need the full API or the reduced API, I reference project A or B correspondingly.

I would like to write extension methods for the ODataService class in a seperate project C, such that I can use these extension methods no matter if I choose to reference project A (full API) or project B (reduced API).

What would be a good approach for this?


回答1:


One possible solution would be:

  1. Create an interface for ODataService-s (e.g.: IODataService)
  2. Create partial classes for your services that implement the interface:
    (e.g.: public partial class ODataService1: IODataService {...})
  3. Create the extension methods for the IODataService


来源:https://stackoverflow.com/questions/65106014/extension-methods-for-two-different-classes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!