考虑以下代码:
class Program
{
static void Main(string[] args)
{
var t = new Test();
t.ToString();
Console.WriteLine(t.ToString());
Console.ReadKey();
}
}
public class Test
{
}
public static class TestHelper
{
public static string ToString(this Test test)
{
return "HashValue of test instance: " + test.GetHashCode();
}
}
我怎样才能调用我的重载(至少 VS 告诉我ToString()有+1 overload)扩展方法?由于Test从 .NET Framework 继承(沿 .NET Framework afaik 中的所有类)object。该类object具有ToString()返回类似<namespace-wher-the-class-is-locatet.<class-name>. 但是,如果我想要ToString()自己的实现不带其他参数怎么办?
不,我不想ToString()在课堂上实施。我想把它作为一种扩展方法。(就像 SecureString 的扩展,将其从非托管、加密转换回托管、解密)
提前致谢!