将 this 作为静态方法中的参数传递

发布于 2024-12-26 04:26:35 字数 455 浏览 3 评论 0原文

我在使用 Visual C# for Windows Phone 中的某些代码时遇到一些问题 问题不是它不起作用,因为它确实起作用,但我不明白如何=P 在静态类中,创建了一个静态方法,该方法将自身作为参数:

public static void MethodONe( this Timeline animation )
{
    //this class does not extend the TimeLine class, and is not connected to it in any                   
    //such way.
    animation.MethodTwo( );
}

public static void MethodTwo( this Timeline animation )
{
    someCode( );
}

这个参数传递是如何调用的,它到底是做什么的?

I'm having some trouble with some code in Visual C# for Windows Phone
The trouble is not that it does not work, because it does, but I don't understand how =P
Within a static class, a static method is created, which gives itself as a parameter:

public static void MethodONe( this Timeline animation )
{
    //this class does not extend the TimeLine class, and is not connected to it in any                   
    //such way.
    animation.MethodTwo( );
}

public static void MethodTwo( this Timeline animation )
{
    someCode( );
}

How is this parameterpassing called, and what does it do, exactly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

自由范儿 2025-01-02 04:26:36

这是所谓的 Timeline 对象的扩展方法。它添加功能而不修改类本身。

http://msdn.microsoft.com/en-us/library/bb383977.aspx

在您的情况下,动画参数是 Timeline 对象(正在调用该函数):

var timeLine = new Timeline();
timeLine.MethodTwo();

因此 timeLine 对象将作为动画参数传递到函数中。
维基百科上有一篇很好的文章,详细解释了它:

http://en.wikipedia.org/wiki/扩展方法

This is a so called extention method to the Timeline object. It adds functionallity without modifying the class itself.

http://msdn.microsoft.com/en-us/library/bb383977.aspx

And in your case the animation parameter is the Timeline object (which is calling the function):

var timeLine = new Timeline();
timeLine.MethodTwo();

So the timeLine object will be passed as the animation parameter into the function.
There's a nice article on wikipedia which explains it futher in detail:

http://en.wikipedia.org/wiki/Extension_method

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文