使用扩展方法扩展字符串(C#3.0)?

Bob*_*lan 4 c# extension-methods

我知道我可以像这样扩展字符串类:

public static class EMClass
{
   public static int ToInt32Ext(this string s)
   {
      return Int32.Parse(s);
   }
   public static int ToInt32Static(string s)
   {
      return Int32.Parse(s);
   }
}
Run Code Online (Sandbox Code Playgroud)

并像这样使用它:

string x = "12";
x.ToInt32Ext()
Run Code Online (Sandbox Code Playgroud)

但是我怎么能把它带到我可以这样使用的地方:

int x = string.ToInt32("15");
Run Code Online (Sandbox Code Playgroud)

And*_*ell 12

我不认为你可以做"静态"扩展方法.

  • 那就对了.Microsoft已多次向Microsoft建议此功能(Microsoft Connect),但不会在不久的将来添加. (2认同)