I'm having a problem with an interface, and I'm not sure how to resolve it.
Here's the scenario:
// IApplicationForm does nothing other than ensure it's an
// application form.
public class MortgageApplicationForm : IApplicationForm {}
internal interface IDataAdapter
{
StringContent FormatOutput<TForm>(TForm form) where TForm : IApplicationForm;
}
internal class DataAdapter : IDataAdapter
{
public StringContent FormatOutput<TForm>(MortgageApplicationForm form)
where TForm : IApplicationForm
{
return new StringContent("", Encoding.UTF8, MediaType.Json.Description());
}
}
DataAdapter
is the non-generic DataAdapter for MortgageApplicationForm
, so I'd like to use the concrete class rather than the IApplicationForm
interface for the FormatOutput
method.
However, I get a message to say that IDataAdapter
doesn't implement the method with that signature.
I understand that <TForm>(TForm form)
isn't the same as <TForm>(MortgageApplicationForm form)
but I thought it would be acceptable because MortgageApplicationForm
implements the IApplicationForm
interface.
I was wrong - any advice appreciated.
Update
Scott's solution is correct but it doesn't work in this instance because of the way DataAdapter is instantiated using Reflection:
public static IDataAdapter GetDataAdapter(string apiKey)
{
return (IDataAdapter)Activator.CreateInstance(
Type.GetType($"My.Base.Namespace.{apiKey}.DataAdapter.cs"));
}