title: C# conditional attribute on interface member
body: <p>I'm trying to get rid of the "#if TRACE" directives in my code, by using the Conditional attribute instead, but can't apply this approach easily to interfaces. I have a way round this but it's pretty ugly, and I'm looking for a better solution.</p>
  
  <p>E.g. I have an interface with a conditionally compiled method.</p>
  
  <pre><code>interface IFoo
  {
  #if TRACE
      void DoIt();
  #endif
  }
  </code></pre>
  
  <p>I can't use the conditional attribute in an interface:</p>
  
  <pre><code>// Won't compile.
  interface IFoo
  {
      [Conditional("TRACE")]
      void DoIt();
  }
  </code></pre>
  
  <p>I could have the interface method just call a conditional private method in the concrete class:</p>
  
  <pre><code>interface IFoo
  {
      void TraceOnlyDoIt();
  }
  
  class Foo : IFoo
  {
      public void TraceOnlyDoIt()
      {
          DoIt();
      }
  
      [Conditional("TRACE")]
      void DoIt()
      {
          Console.WriteLine("Did it.");
      }
  }
  </code></pre>
  
  <p>This would leave my client code with redundant calls to the 'nop' TraceOnlyDoIt() method in a non-TRACE build. I can get round that with a conditional extension method on the interface, but it's getting a bit ugly.</p>
  
  <pre><code>interface IFoo
  {
      void TraceOnlyDoIt();
  }
  
  class Foo : IFoo
  {
      public void TraceOnlyDoIt()
      {
          Console.WriteLine("Did it.");
      }
  }
  
  static class FooExtensions
  {
      [Conditional("TRACE")]
      public static void DoIt(this IFoo foo)
      {
          foo.TraceOnlyDoIt();
      }
  }
  </code></pre>
  
  <p>Is there a better way to do this?</p>