Teraz moim celem była jedna para z typem podstawowym i definicją typu (wymaganie A). Dla definicji typu chcę używać dziedziczenia (Wymaganie B). Użycie powinno być możliwe bez wyraźnej wiedzy o typie bazowym (wymaganie C).
Po Teraz wiem, że gernic ograniczenia nie są wykorzystywane do rozwiązywania ogólny typ zwracany, eksperymentowałem trochę:
introducte
OK Chodźmy Get2:
class ServiceGate
{
public IAccess<C, T> Get1<C, T>(C control) where C : ISignatur<T>
{
throw new NotImplementedException();
}
public IAccess<ISignatur<T>, T> Get2<T>(ISignatur<T> control)
{
throw new NotImplementedException();
}
}
class Test
{
static void Main()
{
ServiceGate service = new ServiceGate();
//var bla1 = service.Get1(new Signatur()); // CS0411
var bla = service.Get2(new Signatur()); // Works
}
}
w porządku, ale to rozwiązanie biegu nie requriement B.
Następny try:
class ServiceGate
{
public IAccess<C, T> Get3<C, T>(C control, ISignatur<T> iControl) where C : ISignatur<T>
{
throw new NotImplementedException();
}
}
class Test
{
static void Main()
{
ServiceGate service = new ServiceGate();
//var bla1 = service.Get1(new Signatur()); // CS0411
var bla = service.Get2(new Signatur()); // Works
var c = new Signatur();
var bla3 = service.Get3(c, c); // Works!!
}
}
Nice! Teraz kompilator może wywnioskować ogólne typy zwracane. Ale nie podoba mi się to. Inne try:
class IC<A, B>
{
public IC(A a, B b)
{
Value1 = a;
Value2 = b;
}
public A Value1 { get; set; }
public B Value2 { get; set; }
}
class Signatur : ISignatur<bool>
{
public string Test { get; set; }
public IC<Signatur, ISignatur<bool>> Get()
{
return new IC<Signatur, ISignatur<bool>>(this, this);
}
}
class ServiceGate
{
public IAccess<C, T> Get4<C, T>(IC<C, ISignatur<T>> control) where C : ISignatur<T>
{
throw new NotImplementedException();
}
}
class Test
{
static void Main()
{
ServiceGate service = new ServiceGate();
//var bla1 = service.Get1(new Signatur()); // CS0411
var bla = service.Get2(new Signatur()); // Works
var c = new Signatur();
var bla3 = service.Get3(c, c); // Works!!
var bla4 = service.Get4((new Signatur()).Get()); // Better...
}
}
Moje ostateczne rozwiązanie jest mieć coś ISignature<B, C>
, gdzie B ist typu bazowego i C definicji ...
może dodawania błędu można dostać ... – vulkanino
odpowiedzi Eric Lippert za tutaj, wraz ze swoim blogiem, do którego prowadzi link, jest całkiem dobrym wyjaśnieniem, dlaczego. Zasadniczo, jeśli dobrze pamiętam, kompilator nie wywnioskuje typów przy użyciu ogólnych ograniczeń.http://stackoverflow.com/questions/3630153/generic-extension-method-type-argument-cannot-be-inferred-from-the-usage/3630202#3630202 –