Code: Select all
public interface IQuery
{
}
public record MyResult(string Name);
public record MyQuery(int Number) : IQuery;
{
public TResult PerformQuery(TQuery query)
where TQuery : IQuery
{
... lookup stuff here ...
}
}
< /code>
Jetzt verwende ich das Repository in meinem Programm: < /p>
internal class Program
{
static void Main(string[] args)
{
var repository = new Repository();
var query = new MyQuery(25);
// This is what I have to write. It is clumsy.
var result = repository.PerformQuery(query);
// This is what I wanted to write. The compiler knows the type of query and should thus know the type of result.
// var result = repository.PerformQuery(query);
}
}
< /code>
Ich möchte das Repository aufrufen, ohne die generischen Typen explizit zu machen, aber leider kann der Compiler die generischen Typen selbst nicht herausfinden. Dies wird nicht kompilieren: < /p>
// Does not compile
var result = repository.PerformQuery(query);
< /code>
Zwei Fragen: < /p>
Was ist los - Warum ist es für den Compiler unmöglich, die Typen selbst zu berechnen?>