This is not possible for the following reasons:
- Starting with C # 4, all-or-nothing output - the compiler cannot output some common arguments, but not others.
- With C # 4, it is not possible to specify common "wildcards", such as
where TConcrete : Base<???> .
Here are some workarounds.
Not a common type of base . Create a base class or interface type that is not shared. This is the big picture; e.g. IEnumerable<T> : IEnumerable .
Covariant interface . With covariance of common C # 4 code, you can create a type-safe solution that does not require cluttering your types with ugly non-native members:
public interface IBase<out TElement> { TElement Element { get; } } class Base<TElement> : IBase<TElement> { public TElement Element { get; set; } } class Concrete : Base<string> { }
And then:
// Won't work with value types. public TConcrete DoSomething<TConcrete>() where TConcrete : IBase<object> { }
And call it that:
var item = DoSomething<Concrete>();
source share