I have a generic type:
public class Source<T> where T : ISomeInterface<X> //...
Now, my problem is, I really don't want to modify Source<T>
to Source<T,X>
, but I want to use X inside Source.
Is it possible in any way?
The actual type arguments of a generic type are. reference types, wildcards, or. parameterized types (i.e. instantiations of other generic types).
Not really. You need to use reflection, basically. Generics are really aimed at static typing rather than types only known at execution time.
You can constrain the generic type by interface, thereby allowing only classes that implement that interface or classes that inherit from classes that implement the interface as the type parameter.
You can't inherit from the generic type parameter.
No, there's no way of expressing that. If you want to be able to refer to X
within Source
, it has to be a type parameter.
Bear in mind that T
could implement (say) ISomeInterface<string>
and ISomeInterface<int>
. What would X
be in that case?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With