Glo*_*tin 6 c# vb.net interface vb.net-to-c#
我正在使用VB编写的Web服务并用C#重写它.服务类实现两个接口,每个接口有四个方法.四种方法中的三种具有相同的签名方法名称和参数列表.第四种方法具有相同的名称,但具有不同的参数列表.
在VB中,您明确标识与公共服务类方法关联的接口方法.因此,对于相同的方法,实现如下所示:
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
}
Run Code Online (Sandbox Code Playgroud)
你能用C#做这个吗?
我知道您可以使用Interface.Method2(Id as Int64,...)和Interface2.Method2(In as ...)显式定义Web服务类中的方法.但这意味着要更改这些方法的名称,因此必须更新使用这些方法的任何应用程序.
我还可以更改接口方法的名称以匹配Web服务方法,但是必须更改使用这些接口的任何应用程序.
是否有任何明确标识Web服务类中的接口方法,但保持Web服务类方法与原始方法名称和签名相同?
不幸的是,在C#中没有直接等同于这个VB.NET功能.C#中没有办法实现接口方法并给它一个不同的名称.这可以通过创建所需的名称并将接口实现转发到该方法名称来进行模拟
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); }
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1828 次 |
| 最近记录: |