【问题标题】:What does "this" refer to in a C# method signature and is there a VB.NET equivalent?“this”在 C# 方法签名中指的是什么,是否有 VB.NET 等价物?
【发布时间】:2025-12-01 15:40:01
【问题描述】:

我一直在观看ASP.NET MVC Storefront 视频系列再次,并看到了一些我以前从未注意到或关注过的东西。我注意到在各种方法的签名列表中有很多对this 的引用。这是一个例子:

public static Category WithCategoryName(this IList<Category> list, string categoryName)   
{
    return 
    (
        from s in list
        where s.Name.Equals(categoryName, StringComparison.InvariantCultureIgnoreCase)
        select s
    )
    .SingleOrDefault();
}

我立即明白签名中的IList&lt;Category&gt; liststring categoryName,但对this 的作用感到困惑。

所以,作为一个 95% 的 VB 人,我将代码弹出到我最喜欢的转换器中并得到:

<System.Runtime.CompilerServices.Extension>
Public Shared Function WithCategoryName(list As IList(Of Category), categoryName As String) As Category

    Return 
    (
        From s In list 
        Where s.Name.Equals(categoryName, StringComparison.InvariantCultureIgnoreCase)
        Select s
    )
    .SingleOrDefault()

End Function

首先,我不完全确定为什么包含&lt;System.Runtime.CompilerServices.Extension&gt;,也许它只是转换器,但是,正如你所看到的,this 没有转换成任何我可以告诉的东西,除非它必须使用前面提到的&lt;System.Runtime.CompilerServices.Extension&gt;

所以问题是:

  1. this 在 C# 方法签名中实际指代和/或做什么?
  2. 是否有 VB.NET 等价物?



对问题 1 的回应:

所以我们已经明确澄清 this 实际上 表示扩展方法,从给出的答案来看,似乎没有内联 VB 等价物。

我想补充一点,因为我提到了 ASP.NET MVC Storefront 视频,所以上面的 C# 示例是从他的 CategoryFilters 类中提取的。我假设这就是您实现被称为 管道和过滤器管道 方法的方法。



对问题 2 的回应:

我假设 VB.NET 处理扩展方法的方式是这样的,例如:

Imports System.Runtime.CompilerServices 

Public Module StringExtensions 

    <Extension()> _ 
    Public Function IsNullOrBlank(ByVal s As String) As Boolean 
       Return s Is Nothing OrElse s.Trim.Length.Equals(0) 
    End Function 

End Module

【问题讨论】:

标签: c# vb.net signature c#-to-vb.net


【解决方案1】:

这是一种扩展方法。 this 指定它是this &lt;parameter&gt; 类型的扩展方法,在您的情况下为IList&lt;Category&gt;

There is a VB.NET equivalent here,虽然它是一个属性,而不是关键字。

扩展方法需要知道要应用的类型,注意这在泛型中很明显。扩展方法:

public static string GetNameOf(this List<Category> category) { return ""; }

List&lt;Category&gt; 之外,其他任何内容均不可用。

【讨论】:

    【解决方案2】:

    出现在那个地方意味着Extension Method

    namespace ExtensionMethods
    {
        public static class MyExtensions
        {
            public static int WordCount(this String str)
            {
                return str.Split(new char[] { ' ', '.', '?' }, 
                                 StringSplitOptions.RemoveEmptyEntries).Length;
            }
        }   
    }
    

    在这段代码之后任何你程序中的字符串对象都可以使用这个函数,比如

    int count = "Hello world".WordCount();  //count would be equal 2
    

    换句话说,这是一种扩展您无权访问或不允许更改或派生的类型的功能的方法。

    【讨论】:

      【解决方案3】:

      这会创建一个扩展方法。

      VB.Net对此没有对应的语法,所以需要自己应用属性。

      【讨论】:

      • 哦,好的,好的。我在同一页上。 VB 的等价物并不是真正的内联等价物,但从某种意义上说,您可以创建一个 Module 类,该类将一组扩展分组到您想要的任何类上。例如:Imports System.Runtime.CompilerServices Public Module StringExtensions &lt;Extension()&gt; _ Public Function IsNullOrBlank(ByVal s As String) As Boolean Return s Is Nothing OrElse s.Trim.Length.Equals(0) End Function End Module
      • 没错。 (注意.Net 已经有String.IsNullOrWhitespace
      • 是的,我知道,但是在我的简单扩展模块中我不想传递参数,我希望它链接任何现有的字符串。
      • 感谢您的第一个和正确的回复,但为了以后的访客着想,由于他的解释并链接到 VB 等价物,我将把 @AdamHouldsworth 的答案标记为答案。
      最近更新 更多