title: C# - implementing interface methods with different complex input parameters, per implementation
body: <p>What I want to achieve is simple: for an interface like so:</p>
  
  <pre><code>public interface ITestInterface
  {
      void SomeMethod(InputDataBaseClass inputData);
  }
  </code></pre>
  
  <p>I would like to have multiple implementations like so:</p>
  
  <pre><code>public class TestInterfaceImp1 : ITestInterface
  {
      public void SomeMethod(InputDataDerivedClass1 inputData)
      {
  
      }
  }
  
  public class TestInterfaceImp2 : ITestInterface
  {
      public void SomeMethod(InputDataDerivedClass2 inputData)
      {
  
      }
  }
  </code></pre>
  
  <p>and so on. <strong>InputDataDerivedClass1</strong> and <strong>InputDataDerivedClass2</strong> are derived from <strong>InputDataBaseClass</strong>:</p>
  
  <pre><code>public class InputDataBaseClass
  {
  
  }
  
  public class InputDataDerivedClass1 : InputDataBaseClass
  {
  
  }
  
  public class InputDataDerivedClass2 : InputDataBaseClass
  {
  
  }
  </code></pre>
  
  <p>So, in the end I would be able to declare something like:</p>
  
  <pre><code>public class SomeClass
  {
      public ITestInterface TestInterface { get; set; }
  }
  </code></pre>
  
  <p>Inject either <strong>TestInterfaceImp1</strong> or <strong>TestInterfaceImp2</strong>, and call <strong>SomeMethod</strong> with the appropriate input data type.</p>
  
  <p>OK, I realize we can't do something like this directly because method signatures on interface implementations must match. I also realize that the common workaround is to declare a generic interface like so:</p>
  
  <pre><code>public interface ITestInterface&lt;T&gt; where T:InputDataBaseClass
  {
      void SomeMethod(T inputData);
  }
  
  public class InputDataBaseClass : ITestInterface&lt;InputDataConcreteClass1&gt;
  {
      public void SomeMethod(InputDataConcreteClass1 inputData)
      {
  
      }
  }
  </code></pre>
  
  <p>However, this won't compile:</p>
  
  <pre><code>ITestInterface&lt;InputDataBaseClass&gt; t = new InputDataBaseClass();
  </code></pre>
  
  <p>even though, <strong>InputDataConcreteClass1</strong> inherits the <strong>InputDataBaseClass</strong>... so I would have to either convert <strong>every</strong> class that uses the <strong>ITestInterface</strong> to a generic:</p>
  
  <pre><code>public class SomeClass&lt;T&gt;
  {
      public ITestInterface&lt;T&gt; TestInterface { get; set; }
  }
  </code></pre>
  
  <p>OR declare the interface type explicitly:</p>
  
  <pre><code>public class SomeClass
  {
      public ITestInterface&lt;InputDataConcreteClass1&gt; TestInterface { get; set; }
  }
  </code></pre>
  
  <p>Which is counterproductive to the whole idea... Or am I missing something? </p>
  
  <p><strong>I guess my question is:</strong> is there any better approach? Or is that a limitation of interfaces in C# and there is no way to have a method that takes different input data for different implementation?</p>
  
  <p><strong>Edit1:</strong></p>
  
  <p>OK, I know I can do that using casting inside the implementation:</p>
  
  <pre><code>public class Impl1: ITestInterfaceT
  {
      public void SomeMethod(InputDataBaseClass inputData)
      {
          InputDataDerivedClass1 cast = inputData as InputDataDerivedClass1;
  
          System.Console.WriteLine(cast.A);
          System.Console.WriteLine(cast.B);
      }
  }
  
  public class Impl2 : ITestInterfaceT
  {
      public void SomeMethod(InputDataBaseClass inputData)
      {
          InputDataDerivedClass2 cast = inputData as InputDataDerivedClass2;
  
          System.Console.WriteLine(cast.A);
          System.Console.WriteLine(cast.C);
      }
  }
  
  public interface ITestInterfaceT
  {
      void SomeMethod(InputDataBaseClass inputData);
  }
  
  public class InputDataBaseClass
  {
      public string A { get; set;}
  }
  
  public class InputDataDerivedClass1 : InputDataBaseClass
  {
      public string B { get; set;}
  }
  
  public class InputDataDerivedClass2 : InputDataBaseClass
  {
      public string C { get; set;}
  }
  </code></pre>
  
  <p>But this is exactly what I'm trying to avoid; without compile-time type checking, this will be a rather fragile code - is there any better way?</p>
  
  <p><strong>Edit2:</strong></p>
  
  <p>The problem I'm facing is that the implementation of my interface has to implement a method that processes the same logic but use slightly different input data in each implementation. <strong>For example</strong>, I have method <strong>SaveData</strong> - one implementation saves it to the database and requires data plus a set of foreign keys, while the other saves it to the disk and requires data plus the file name. Until now, I always did it by inserting all required data in the constructor of the implementation and then calling the SaveData method (which was either parameterless or with a single parameter), like so:</p>
  
  <pre><code>public interface IDataSaver
  {
      void SaveData(string someData);
  }
  
  public class ToDiskDataSaver : IDataSaver
  {
      private string FilePath { get; set;}
  
      public ToDiskDataSaver(string filePath)
      {
          this.FilePath = filePath;
      }
  
      public void SaveData(string someData)
      {
          //save file to disk here
      }
  }
  
  public class ToDatabaseDataSaver : IDataSaver
  {
      private long DataForeignKey { get; set; }
  
      public ToDatabaseDataSaver(long dataForeignKey)
      {
          this.DataForeignKey = dataForeignKey;
      }
  
      public void SaveData(string someData)
      {
          //save file to database here
      }
  }
  </code></pre>
  
  <p>and so on.</p>
  
  <p><strong>This works perfectly</strong>... but I'm looking for a way to make it work when a method needs complex input parameters - different for each implementation (without inserting them in the constructor). Perhaps this can't be done...</p>