b0bi
b0bi

Reputation: 628

Generic type in generic constraint

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<>.

Upvotes: 5

Views: 1232

Answers (3)

p.s.w.g
p.s.w.g

Reputation: 149078

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.

Upvotes: 5

Eric Scherrer
Eric Scherrer

Reputation: 3408

The problem is you think FilterControl<> is a base class. It is not, think of it as a template. It has no substance until it is given the context of what is inside the <>.

Upvotes: 0

dcastro
dcastro

Reputation: 68750

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()
{
}

Upvotes: 2

Related Questions