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

时间:2012-01-11 12:26:11

标签: c# this parameter-passing

我在使用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( );
}

这个参数传递是如何被调用的,它究竟是做什么的?

1 个答案:

答案 0 :(得分:1)

这是对Timeline对象的所谓扩展方法。它增加了功能,而无需修改类本身。

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

在你的情况下,animation参数是Timeline对象(调用函数):

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

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

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