У меня есть следующий метод:
public TResult Get<TGenericType, TResult>()
where TGenericType : SomeGenericType<TResult>
where TResult : IConvertible {
//...code that uses TGenericType...
//...code that sets someValue...
return (TResult) someValue;
}
Прямо сейчас, пользователь этого метода должен использовать его так:
//Notice the duplicate int type specification
int number = Get<SomeGenericType<int>, int>();
Почему я должен указывать TResult в определении метода? Компилятор уже знает TResult, так как я указал его в TGenericType. В идеале (если бы компилятор C # был немного умнее), мой метод должен выглядеть следующим образом:
public TResult Get<TGenericType>()
where TGenericType : SomeGenericType<TResult>
where TResult : IConvertible {
//...code that uses TGenericType...
//...code that sets someValue...
return (TResult) someValue;
}
Таким образом, пользователь может просто использовать это так:
//Much cleaner
int number = Get<SomeGenericType<int>>();
Есть ли способ сделать то, что я хочу сделать?