【发布时间】:2010-08-20 22:16:37
【问题描述】:
我有点困惑为什么这不会出错。我在一些过时的遗留软件的深处发现了这段代码,并惊讶地发现它可以工作。
public static string CleanFileName(this string fileName)
{
return CleanFileName(fileName, 64);
}
public static string CleanFileName(this string fileName, int maxLength)
{
//some logic
}
我对扩展方法的经验是这样称呼它:
fileName.CleanFileName(64);
这仅仅是因为它也是一个静态方法吗?这是常见的做法,只是我还没有看到的东西,还是我应该用火杀死的过时的遗留代码?
【问题讨论】:
-
为什么你认为这不起作用? msdn.microsoft.com/en-us/library/bb383977.aspx 明确指出
Extension methods are a special kind of static method, but they are called as if they were instance methods on the extended type。
标签: c# static extension-methods this legacy-code