繁体   English   中英

接口方法使用ByRef和VB.NET实现和传递

[英]Interface method Implementation and passing using ByRef with VB.NET

我有一个C#接口定义如下:

public interface IMenuSecurityService
{
    void SetSecurityFlags(List<MenuItem> items);
}

我需要在VB.NET类中实现这个接口。 当我使用ByVal传递的items参数实现SetSecurityFlags方法时,它会编译。

Public Sub SetSecurityFlags(ByVal items As List(Of L1.Common.Model.MenuItem)) Implements IMenuSecurityService.SetSecurityFlags
    ' Do some work
End Sub

当我尝试使用ByRef传递的items参数实现它时,我得到以下编译器错误:

类'UserRights'必须实现'Sub SetSecurityFlags(items As System.Collections.Generic.List(Of Model.MenuItem))'for interface

Public Sub SetSecurityFlags(ByRef items As List(Of L1.Common.Model.MenuItem)) Implements IMenuSecurityService.SetSecurityFlags
    ' Do some work
End Sub

我似乎无法想出这个。 VB.NET不支持这个还是我做错了什么?

这不是VB vs C#问题 - 这是你的签名错误的问题。

您的方法不实现接口,因为接口方法具有by-value参数,而您的方法具有by-reference参数。 如果在C#中执行此操作,您将遇到同样的问题:

interface IFoo
{
    void Bar(int x);
}

class Foo : IFoo
{
    public void Bar(ref int x) {}
}

编译错误:

Test.cs(8,7): error CS0535: 'Foo' does not implement interface member
    'IFoo.Bar(int)'

您还没有解释为什么您认为需要使用ByRef参数 - 当涉及到像List<T>这样的引用类型时,您可能并不真正理解pass-by-reference和pass-by-value语义。 有关更多信息,请查看关于该主题的文章

暂无
暂无

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

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