"本"在功能参数 [英] "this" in function parameter

查看:127
本文介绍了"本"在功能参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

综观为HtmlHelpers一些code的例子,我看到的声明是这样的:

Looking at some code examples for HtmlHelpers, and I see declarations that look like:

public static string HelperName(this HtmlHelper htmlHelper, ...more regular params )

我不记得看​​到这种类型的构造任何别的地方 - 有人可以解释的本的目的是什么?我认为,通过声明一些公共静态意味着类并不需要实例化 - ?那么什么是本在这种情况下

I can't remember seeing this type of construct any where else - can someone explain the purpose of the "this"? I thought that by declaring something public static meant that the class did not need to be instantiated - so what is "this" in this case?

推荐答案

这是用于声明扩展方法的C#3.0中的新功能的语法。

This is the syntax for declaring extension methods, a new feature of C# 3.0.

这是扩展方法是一部分code,部分编译器神奇,其中在Visual Studio智能感知的帮助下,编译器使它看起来你的扩展方法实际上可作为讨论的对象上的实例方法。

An extension method is part code, part compiler "magic", where the compiler with the help of intellisense in Visual Studio make it appear that your extension method is actually available as an instance method on the object in question.

让我举一个例子。

有一个关于名为GobbleGobble String类没有一种方法,让我们创建一个扩展方法:

There's no method on the String class that is named GobbleGobble, so let's create an extension method:

public static class StringExtensions
{
    public static void GobbleGobble(this string s)
    {
        Console.Out.WriteLine("Gobble Gobble, " + s);
    }
}

类的名称只是我的命名约定,也没有必要将其命名为这样的,但它是静态的,因为这样做的方法。

The class name is just my naming convention, it isn't necessary to name it like that, but it has to be static, as do the method.

宣布上述方法后,您可以在Visual Studio中,键入:

After declaring the above method, you can, in Visual Studio, type this:

String s = "Turkey Baster!";
s.

点后,等待智能感知,并注意有一个GobbleGobble方法有,完成code是这样的:

after the dot, wait for intellisense, and notice there is a GobbleGobble method there, complete the code like this:

String s = "Turkey Baster!";
s.GobbleGobble();

重要提示:其中扩展方法被声明的类必须可用于编译器和智能感知处理器,以便智能感知显示方法。如果您在GobbleGobble手动输入,并使用<大骨节病>控制 + <大骨节病>。快捷方式,它不会帮助你使用指令到文件的权利。

Important: The class where the extension method is declared must be available to the compiler and the intellisense processor in order for intellisense to show the method. If you type in GobbleGobble manually, and use the Ctrl+. shortcut, it will not help you get the right using directives into the file.

注意,参数的方法已经消失了。编译器会悄悄地走动重要的位,它们是:

Notice that the parameter to the method has disappeared. The compiler will silently move around the important bits, which are:

String s = "Turkey Baster!";
s.GobbleGobble();
^     ^
|     +-- the compiler will find this in the StringExtensions class
|
+-- will be used as the first parameter to the method

因此​​,上述code将被由编译器变换为这样:

Thus, the above code will be transformed by the compiler to this:

String s = "Turkey Baster!";
StringExtensions.GobbleGobble(s);

因此​​,在通话时,有什么神奇之处,它只是一个静态方法的调用。

So at call-time, there's nothing magical about it, it's just a call to a static method.

请注意,如果你的扩展方法声明多个参数,只有第一个支持这个修改,其余的已被指定为方法调用的一部分正常:

Note that if your extension method declares more than one parameter, only the first supports the this modifier, and the rest has to be specified as part of the method call as normal:

public static void GobbleGobble(this string value, string extra)
{                                            |              |
    ...                                      |              |
}                                            |              |
                                             |              |
+--------------------------------------------+              |
|                                                           |
v                                                           |
s.GobbleGobble("extra goes here");                          |
                        ^                                   |
                        |                                   |
                        +-----------------------------------+

部分加入,由于LINQ的,其中C#的LINQ的语法会寻找适当命名的扩展方法在游戏中的对象,这意味着你可以通过只宣称引进的LINQ支持任何类型的类

扩展方法正确的扩展方法。当然,完全支持LINQ是大量的工作,但它是可能的。

Extension methods was added in part due to Linq, where the Linq syntax of C# will look for appropriately named extension methods for the objects in play, which means you can "introduce" Linq-support into any type of class by just declaring the right extension methods. Of course, full Linq support is a lot of work, but it is possible.

此外,通过自己的扩展方法是真正有用的,所以读了它。

Also, extension methods by themselves are really useful, so read up on it.

下面是几个链接:

  • MSDN: Extension Methods (C# Programming Guide)
  • MSDN Magazine - Basic Instincts: Extension Methods
  • Wikipedia: Extension Method

这篇关于&QUOT;本&QUOT;在功能参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆