问题
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:
- Create an interface for
ODataService
-s (e.g.:IODataService
) - Create partial classes for your services that implement the interface:
(e.g.:public partial class ODataService1: IODataService {...}
) - Create the extension methods for the
IODataService
来源:https://stackoverflow.com/questions/65106014/extension-methods-for-two-different-classes