“这个"在函数参数中 [英] "this" in function parameter

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

问题描述

查看 HtmlHelpers 的一些代码示例,我看到如下声明:

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

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

我不记得在其他任何地方看到过这种类型的构造 - 有人可以解释这个"的目的吗?我认为通过声明一些 public static 意味着不需要实例化该类 - 那么在这种情况下什么是this"?

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.

扩展方法是部分代码,部分是编译器魔术",其中编译器在 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.

让我举个例子.

String 类上没有名为 GobbleGobble 的方法,所以让我们创建一个扩展方法:

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.

在点之后,等待intellisense,注意那里有一个GobbleGobble方法,像这样完成代码:

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,并使用 Ctrl+. 快捷方式,它不会帮助您在文件中正确使用指令.

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

因此,上面的代码会被编译器转换成这样:

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.

请注意,如果您的扩展方法声明了多个参数,则只有第一个支持 this 修饰符,其余的必须正常指定为方法调用的一部分:

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.

这里有几个链接:

这篇关于“这个"在函数参数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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