参加 2 个课程 - 本质上

发布于 2024-08-20 03:42:39 字数 294 浏览 3 评论 0原文

我有一个对第 3 方 WSDL 的 .NET Web 服务参考。

该参考文献中有 2 个类。基本上,这 2 个类很可能是第 3 方 API 端的接口,但在 .NET 中最终成为 2 个代理类。

我需要将这两个类合并为一个类。为什么?因为将它们分开是愚蠢的,所以它们是允许我进行方法调用的服务。方法调用都在这两个代理类之间各占一半。

因此,我想创建一个名为 ThirdPartyService 的自定义包装类,并以某种方式本质上继承这些代理类的成员。我知道你可以在 C# 中继承 2 个类,但我也不知道如何使用接口来做到这一点。

I've got a .NET Web Service Reference to a 3rd party WSDL.

In that reference are 2 classes. Basically these 2 classes are most likely Interfaces on the 3rd Party API side but in .NET end up as 2 proxy classes.

I have a need to combine both these classes into one class. Why? Because it's stupid that these are split, they're the service which allows me to make method calls. The method calls are all split half and half between these 2 proxy classes.

So I want to create a custom wrapper class called ThirdPartyService and somehow essentially inherit both those proxy class's members. I know you can inherit 2 classes in C# but I don't see how to do this with an interface either.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

不必你懂 2024-08-27 03:42:39

好吧,你没有给我们太多的帮助,但你可以使用组合来做到这一点。

class Foo {
    public void Frobber();
}

class Bar {
    public void Blorb();
}

那么

class FooAndBar {
    Foo _foo;
    Bar _bar;
    public FooAndBar(Foo foo, Bar bar) {
        _foo = foo;
        _bar = bar;
    }

    public void Frobber() { _foo.Frobber(); }
    public void Blorb() { _bar.Blorb(); }
}

如果 FooBar 分别实现接口 IFooIBar 那么你就可以拥有 FooAndBar 也实现 IFooIBar

设计模式的语言来说,我们将其称为适配器模式

关于您的编辑:

<块引用>

我知道你可以在 C# 中继承 2 个类,但我也不知道如何使用接口来做到这一点。

不可以。您不能从 C# 中的两个类继承。你必须像我已经解释过的那样使用组合。

Well, you haven't given us a lot to work with, but you could do this using composition.

class Foo {
    public void Frobber();
}

class Bar {
    public void Blorb();
}

Then

class FooAndBar {
    Foo _foo;
    Bar _bar;
    public FooAndBar(Foo foo, Bar bar) {
        _foo = foo;
        _bar = bar;
    }

    public void Frobber() { _foo.Frobber(); }
    public void Blorb() { _bar.Blorb(); }
}

If Foo and Bar implement interfaces IFoo and IBar respectively then you can have FooAndBar implement IFoo and IBar too.

In the language of Design Patterns we would call this the adapter pattern.

Regarding your edit:

I know you can inherit 2 classes in C# but I don't see how to do this with an interface either.

No. You can not inherit from two classes in C#. You have to use composition like I already explained.

屋顶上的小猫咪 2024-08-27 03:42:39

也许为了给 Jason 的帖子添加一些价值,这类内容也被称为 适配器

Maybe to add some value to Jason's post, this sort of stuff would also be known as adapter

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文