两种不同类的扩展方法

编程入门 行业动态 更新时间:2024-10-17 00:17:56
本文介绍了两种不同类的扩展方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我遇到一种情况,在C#中,我通过OData API消耗数据.实际上,有两个不同的OData端点(两个不同的模型),其中一个是另一个的超集.

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.

我创建了两个不同的C#项目(A和B),其中包含与两个API交互的代码.该代码是使用插入OData Connected服务扩展自动生成的.结果,项目A和项目B都包含一个类 ODataService ,这是要与之交互的主要类型.根据我需要完整的API还是简化的API,我分别引用项目A或B.

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.

我想在单独的项目C中为 ODataService 类编写扩展方法,这样无论我选择引用项目A(完整API)还是项目,我都可以使用这些扩展方法.B(简化的API).

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).

什么是一个好的方法?

推荐答案

一种可能的解决方案是:

One possible solution would be:

  • 为 ODataService -s创建一个接口(例如: IODataService )
  • 为实现接口的服务创建部分类:(例如:公共子类ODataService1:IODataService {...} )
  • 为 IODataService
  • 创建扩展方法
  • 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
  • 更多推荐

    两种不同类的扩展方法

    本文发布于:2023-11-30 16:06:14,感谢您对本站的认可!
    本文链接:https://www.elefans.com/category/jswz/34/1650626.html
    版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
    本文标签:两种   同类   方法

    发布评论

    评论列表 (有 0 条评论)
    草根站长

    >www.elefans.com

    编程频道|电子爱好者 - 技术资讯及电子产品介绍!