簡體   English   中英

在C#.NET的方法簽名中傳遞“ this”的VB.NET等價方式是什么?

[英]What is the VB.NET equivalent of passing 'this' in a method signature in C#.NET?

我的問題來自Wrox Press的Professional ASP.NET Design Patterns的代碼示例。 代碼下載在C#中,但是,我正在VB中研究示例。

如果有人可以解釋以下方法簽名中的“ this”是什么意思,以及VB.NET中的等效方法簽名,我將不勝感激。

這是有問題的代碼示例(來自p.51):

public static void Apply(this IList<Product> products, IDiscountStrategy discountStrategy) { ... }

這是它們在VB.NET中實現的擴展方法

您需要的語法是:

<Extension()> 
Public Sub Print(ByVal aString As String)
    Console.WriteLine(aString)
End Sub

例如

這是一種擴展方法。 這表明您可以使用以下語法調用靜態方法:

  products.Apply(strategy);

相對於

  WhateverClass.Apply(products, strategy);

在VB中,您可以使用Extension屬性裝飾該方法

  <Extension()>
  Sub Apply(ByVal products as IList<Product>, ByVal discountStrategy as IStrategy)
    ...

有關更多信息,請參見http://msdn.microsoft.com/en-us/library/bb384936.aspx

對於VB中的擴展方法,請使用ExtensionAttribute

<Extension()>
Public Sub Apply(IList(of Product) products, IDiscountStrategy discountStrategy)
    '...
End Sub

上面的語法可能不是100%

我認為這是用於擴展方法。 不知道這些如何在VB.NET中實現。

因此, this表示擴展方法。 您現在有了一個Apply方法,該方法可以在任何IList<Product> ,就像它是成員方法一樣,因此您可以像這樣調用它

list.Apply(discountStrategy);

而不是像

Apply(list, discountStrategy);

避免必須將該方法ApplyDiscountStrategyToListOfProducts ,並具有大量Apply*ToListOf*方法,這確實很有用。

VB等效項在這里: http : //msdn.microsoft.com/en-us/library/bb384936.aspx

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM