Два типа c имеют прямое соединение, как избежать повторного ввода? - PullRequest
1 голос
/ 22 апреля 2020
public interface IService<T> where  T : IComparable<T> { }
public class CommonController<S, T> where S : IService<T>, new() where T : IComparable<T> { }

public class CustomerService : IService<int> { }

// Correct
public class CustomerController : CommonController<CustomerService, int> { }

// Confict, from CustomerService, T is int, but the second type is long
public class CustomerController : CommonController<CustomerService, long> { }

очевидно угадать второй тип 'int' из CustomerService. Можно ли удалить второй тип generi c и как?

1 Ответ

3 голосов
/ 22 апреля 2020

Вы можете устранить повторяемость T, выполнив следующее:

public static class Foo<T> where T : IComparable<T>
{
    public interface IService { }
    public class CommonController<S> where S : IService, new() { }
    public class CustomerService : IService { }
    public class CustomerController : CommonController<CustomerService> { }
}

Но если вам нужно определить CustomerService и CustomerController вне Foo<T>, тогда вы в основном вернетесь к квадрат один.

public static class Foo<T> where T : IComparable<T>
{
    public interface IService { }
    public class CommonController<S> where S : IService, new() { }
}

public class CustomerService : Foo<int>.IService { }

public class CustomerController : Foo<int>.CommonController<CustomerService> { }

Таким образом, ответ, в основном, нет.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...