I want to be able to specify something like this:
public abstract class ColumnFilter<TCell, TFilterControl> : ColumnFilter
where TFilterControl : FilterControl<>, new()
where TCell : IView, new()
{
}
Class FilterControl<> is a base class. I can not know what will be the generic argument for FilterControl<>.
A generic type is a generic class or interface that is parameterized over types. The following Box class will be modified to demonstrate the concept.
Declaring those constraints means you can use the operations and method calls of the constraining type. If your generic class or method uses any operation on the generic members beyond simple assignment or calling any methods not supported by System. Object, you'll apply constraints to the type parameter.
Interface Type Constraint 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 use unbound generic types in type constraints. You'd have to add a third type parameter, like this:
public abstract class ColumnFilter<TCell, TFilterControl, TFilterControlType> : ColumnFilter
where TFilterControl : FilterControl<TFilterControlType>, new()
where TCell : IView, new()
{
}
Or create a non-generic base type of FilterControl
:
public FilterControl { }
public FilterControl<T> : FilterControl { }
public abstract class ColumnFilter<TCell, TFilterControl> : ColumnFilter
where TFilterControl : FilterControl, new()
where TCell : IView, new()
{
}
You can also make the base type abstract
with internal
constructors if you want to force consumers to use your generic derived type.
ColumnFilter
will have to be told what that type will be.
Add a third generic type parameter, like so:
public abstract class ColumnFilter<TCell, TFilterControl, TFilter> : ColumnFilter
where TFilterControl : FilterControl<TFilter>, new()
where TCell : IView, new()
{
}
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