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

你离开我真会死。 提交于 2019-12-14 03:33:35

问题


I'm Tryng to made a base class with a base method that order a List with a function that depends on Type.

My Compiler show this Error

Error 13 Impossibile to convert 'System.Linq.Expressions.Expression<System.Func<MLOLPlus.Business.Dealer,string>>' in 'System.Linq.Expressions.Expression<System.Func<T,string>>'. D:\Documenti\Lavori\timage\MLOLPlus\src\MLOLPlus.Business\DataAcess\DataTablesClasses\DataTableMultiSort.cs 197 20 MLOLPlus.Business

IdentityEntity is a base abstract class base of all Custom Class Data type

Example:

  1. User inherits IdentityEntity
  2. Editor too

Base Class MultiSort:

public class DataTableMultiSort
{

    public DataTableParameterModel DataTable { get; set; }

    public IQueryable<T> MultiSort<T>(IQueryable<T> basequery) where T : IdentityEntity{
        return CreateSortedQuery<T>(basequery, DataTable);
    }

    private IOrderedQueryable<T> CreateSortedQuery<T>(IQueryable<T> baseQuery, DataTableParameterModel parameterModel) where T : IdentityEntity
    {
        var orderedQuery = (IOrderedQueryable<T>)baseQuery;

        for (int i = 0; i < parameterModel.iSortingCols; ++i)
        {
            var ascending = string.Equals("asc", parameterModel.sSortDir[i], StringComparison.OrdinalIgnoreCase);
            int sortCol = parameterModel.iSortCol[i];

            Expression<Func<T, string>> orderByExpression = GetOrderingFunc<T>(sortCol);
            if (orderByExpression != null)
            {
                ...do things
            }
            else
            {
                if (ascending)
                {
                    orderedQuery = (i == 0)
                        ? orderedQuery.OrderBy(c => c.Id)
                        : orderedQuery.ThenBy(c => c.Id);
                }
                else
                {
                    ...
                }
            }

        }
        return orderedQuery;
    }

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

}

Custom User Multisort

  public class UserMultiSort : DataTableMultiSort
    {
        protected override Expression<Func<T, string>> GetOrderingFunc<T>(int ColumnIndex)
        {
            Expression<Func<User, string>> InitialorderingFunction;
            switch (ColumnIndex)
            {
                case 1:
                    InitialorderingFunction = c => c.FirstName;
                    break;
                case 2:
                    InitialorderingFunction = c => c.LastName;
                    break;
                default:
                    InitialorderingFunction = null;
                    break;
            }

            return InitialorderingFunction;
        }

    }

Custom Editor Multisort

public class EditorMultiSort : DataTableMultiSort
{
    protected override Expression<Func<T, string>> GetOrderingFunc<T>(int ColumnIndex)
    {
        Expression<Func<Editor, string>> InitialorderingFunction;
        switch (ColumnIndex)
        {
            case 1:
                InitialorderingFunction = c => c.BusinessName;
                break;
            case 2:
                InitialorderingFunction = c => c.Address;
                break;
            default:
                InitialorderingFunction = null;
                break;
        }

        return InitialorderingFunction;
    }

}

回答1:


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


来源:https://stackoverflow.com/questions/26759416/generics-c-sharp-organization-of-methods-that-depends-on-type

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!