且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

我可以扩展方法添加到现有的静态类?

更新时间:2023-12-02 21:51:52

没有。扩展方法需要一个对象的一个​​实例。不过,您可以写周围的 ConfigurationManager中接口的静态包装。如果实施包装,不需要扩展方法,因为你可以直接添加方法。

No. Extension methods require an instance of an object. You can however, write a static wrapper around the ConfigurationManager interface. If you implement the wrapper, you don't need an extension method since you can just add the method directly.

 public static class ConfigurationManagerWrapper
 {
      public static ConfigurationSection GetSection( string name )
      {
         return ConfigurationManager.GetSection( name );
      }

      .....

      public static ConfigurationSection GetWidgetSection()
      {
          return GetSection( "widgets" );
      }
 }