将 this 作为静态方法中的参数传递
我在使用 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是所谓的 Timeline 对象的扩展方法。它添加功能而不修改类本身。
http://msdn.microsoft.com/en-us/library/bb383977.aspx
在您的情况下,动画参数是 Timeline 对象(正在调用该函数):
因此 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):
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