SORU
30 EKİM 2008, PERŞEMBE


Varolan statik bir sınıf uzatma yöntemleri ekleyebilir miyim?

C uzatma yöntemleri hayranıyım# ama herhangi bir başarı statik bir sınıf, Konsol gibi bir uzantısı yöntemi ekleme olmadı.

Eğer Konsol için bir uzantısı eklemek istiyorum, örneğin, 'WriteBlueLine', gidebilmek için:

Console.WriteBlueLine("This text is blue");

'Bu' parametresi... ama nafile! Konsol ile yerel kamu statik bir yöntem ekleyerek denedim,

public static class Helpers {
    public static void WriteBlueLine(this Console c, string text)
    {
        Console.ForegroundColor = ConsoleColor.Blue;
        Console.WriteLine(text);
        Console.ResetColor();
    }
}

Bu 'WriteBlueLine' konsola yöntem... yanlış bir şey mi yaptım? bir daha kapanmadı Ya da imkansızı istiyorsun?

CEVAP
21 Kasım 2008, Cuma


Hayır. Uzantısı yöntemleri bir nesne örneği gerektirir. Ancak, ConfigurationManager arabirimi statik sarıcı yazabilirsiniz. Sen sarıcı uygulamak, sadece yöntem doğrudan ekleyebilirsiniz beri uzantısı bir yöntem gerekmez.

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

      .....

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

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • Evan Coury

    Evan Coury

    29 NİSAN 2007
  • mobilenet.cz

    mobilenet.cz

    26 NİSAN 2008
  • TrenchTech Computer Repair Tutorials

    TrenchTech C

    19 EYLÜL 2014