简体   繁体   English

C#相当于VB.NET接口的实现

[英]C# equivalent of VB.NET interface implementations

I am taking a web service written in VB and rewriting it in C#. 我正在使用VB编写的Web服务并用C#重写它。 The service class implements two interfaces that have four methods each. 服务类实现两个接口,每个接口有四个方法。 Three of the four methods have the same signatures method name and parameter list. 四种方法中的三种具有相同的签名方法名称和参数列表。 The 4th method has the same name, but a different parameter list. 第四种方法具有相同的名称,但具有不同的参数列表。

In VB, you explicitly identify the interface methods associated with the public service class methods. 在VB中,您明确标识与公共服务类方法关联的接口方法。 So for the methods that are the same, implementation looks like this: 因此,对于相同的方法,实现如下所示:

class WebServiceClass
{
   Public Function Method1(result as Int32) As String Implements Interface1.Method1, Interface2.Method1

   Public Sub Method2(Id as Int64, P3 as Int32) Implements Interface1.Method2

   Public Sub Method3(In as Int64) Implements Interface2.Method2
}

Can you do this in C#? 你能用C#做这个吗?

I know you can explicitly define the methods in the web service class with Interface.Method2(Id as Int64, ...) and Interface2.Method2(In as...). 我知道您可以使用Interface.Method2(Id as Int64,...)和Interface2.Method2(In as ...)显式定义Web服务类中的方法。 But that will mean changing the names of these methods and consequently any application that uses these methods will have to be updated. 但这意味着要更改这些方法的名称,因此必须更新使用这些方法的任何应用程序。

I could also change the names of the interface methods to match the web service methods but any application that uses these interfaces will have to be changed. 我还可以更改接口方法的名称以匹配Web服务方法,但是必须更改使用这些接口的任何应用程序。

Is there any any to explicitly identify the interface method in the web service class, but keeping the web service class methods the same name and signature as the original? 是否有任何明确标识Web服务类中的接口方法,但保持Web服务类方法与原始方法名称和签名相同?

Unfortunately there is simply no direct equivalent to this VB.NET feature in C#. 不幸的是,在C#中没有直接等同于这个VB.NET功能。 There is no way in C# to implement an interface method and give it a different name. C#中没有办法实现接口方法并给它一个不同的名称。 This can be simulated though by just creating the name you want and having the interface implementation forward to that method name 这可以通过创建所需的名称并将接口实现转发到该方法名称来进行模拟

class WebServiceClass : Interface1, Interface2
{
  public string Method1(int result) { ... }
  public void Method2(long id, int p3) { ... }
  public void Method3(long in) { ... }

  string Interface1.Method1(int result) { return Method1(result); }
  void Interface1.Method2(long id, int p3) { Method2(id, p3); }
  string Interface2.Method1(int result) { return Method1(result); }
  void Interface2.Method2(long in) { Method3(in); }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM