我有这个命名空间中Extensions.cs
文件:无法访问我exension方法
namespace TextBoxExtensions
{
public static class TextBoxExtensionsClass
{
public static void ResetColor(this Label label)
{
label.ForeColor = new TextBox().ForeColor;
}
}
}
则:
using TextBoxExtensions;
// ...
textbox.ResetColor();
当我尝试访问它在另一个文件中,我得到以下错误:
'System.Windows.Forms.TextBox' does not contain a definition for 'ResetColor' and the best extension method overload 'TextBoxExtensions.TextBoxExtensionsClass.ResetColor(System.Windows.Forms.Label)' has some invalid arguments
你的扩展方法扩展了'Label'类,而不是'TextBox'类。 – 2012-01-05 17:01:05
如果你仔细观察,你会发现编译错误告诉你问题是什么 - 参数的类型不匹配。 – Nuffin 2012-01-05 17:07:53