[Solved] Generics C# organization of methods that depends on type [closed]

Introduction

Solution


The reason you’re getting the compiler error is that you’ve pushed the generic constrains to the individual methods rather than putting the restraint on the class. The way you have it, there’s no way to override the intrinsically generic method GetOrderingFunc<T> with a method that returns a Expression<Func<User, string>>.

What you’ve posted works if you make DataTableMultiSort generic and take the generic paramteres off of the methods:

public class DataTableMultiSort<T> where T : IdentityEntity
{
...
    protected virtual Expression<Func<T, string>> GetOrderingFunc(int ColumnIndex)
    {
        return null;
    }
}

public class UserMultiSort : DataTableMultiSort<User>
{
    protected override Expression<Func<User, string>> GetOrderingFunc(int ColumnIndex)
    {
        Expression<Func<User, string>> InitialorderingFunction;
        ...
     }
}

7