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

问题描述:

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

怎么叫这个parameterpassing,以及它有什么作用,到底是什么?

+0

可能重复[?什么是扩展方法(http://stackoverflow.com/questions/403539/what-are-extension - 方法),[扩展方法](http://stackoverflow.com/questions/5730013/extension-methods),[为什么我们在扩展方法中使用“this”](http://stackoverflow.com/questions/2574311 /为什么我们使用这种扩展方法) – 2012-01-11 12:42:11

这是时间轴对象的所谓扩展方法。它增加了功能而不修改类本身。

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

而在你的情况下,动画参数是Timeline对象(被调用函数):那么Timeline对象将作为动画参数到函数传递

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

。 有维基百科上一个不错的物品,其futher解释得很详细:

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

+0

“......参数这是时间轴对象......” - 这是不正确的。 'this'在这种情况下是_modifier_,而不是_parameter_。参数是'animation' :-) – 2012-01-11 12:41:58

+0

true,编辑文本使其更加清晰:-) – 2012-01-11 12:54:40