[英]What does “this” refer to in a C# method signature and is there a VB.NET equivalent?
[英]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) { ... }
这是一种扩展方法。 这表明您可以使用以下语法调用静态方法:
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.