[英]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.