泛型C#依赖于类型的方法组织 [英] Generics C# organization of methods that depends on type

查看:118
本文介绍了泛型C#依赖于类型的方法组织的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我的编译器显示这个错误

我的编译器显示这个错误

p>

错误13无法在System.Linq.Expressions中转换'System.Linq.Expressions.Expression< System.Func< MLOLPlus.Business.Dealer,字符串>>'。表达式来; System.Func< T,串>>。 D:\Documenti\Lavori\timage\MLOLPlus\src\MLOLPlus.Business\DataAcess\DataTablesClasses\DataTableMultiSort.cs 197 20 MLOLPlus.Business



IdentityEntity是所有自定义类数据类型的基本抽象类基础。例如:

    $ b



    $ b

  1. 用户继承IdentityEntity

  2. 编辑器

基类MultiSort: p>

  public class DataTableMultiSort 
{

public DataTableParameterModel DataTable {get;组; }

public IQueryable< T> MultiSort T(IQueryable T basequery)其中T:IdentityEntity {
返回CreateSortedQuery< T>(basequery,DataTable);
}

private IOrderedQueryable< T>其中,T:IdentityEntity
{
var orderedQuery =(IOrderedQueryable< T>)baseQuery;

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

表达式< Func< T,字符串>> orderByExpression = GetOrderingFunc< T>(sortCol);
if(orderByExpression!= null)
{
...做某事
}
其他
{
if(ascending)
{
orderedQuery =(i == 0)
? orderedQuery.OrderBy(c => c.Id)
:orderedQuery.ThenBy(c => c.Id);
}
else
{
...
}
}

}
return orderedQuery;
}

保护的虚拟表达式< Func< T,字符串>> GetOrderingFunc< T>(int ColumnIndex)其中T:IdentityEntity
{
return null;





自定义用户Multisort

  public class UserMultiSort:DataTableMultiSort 
{
protected override Expression< Func< T,string>> GetOrderingFunc< T>(int ColumnIndex)
{
表达式< Func<用户,字符串>> InitialorderingFunction;
switch(ColumnIndex)
{
case 1:
InitialorderingFunction = c => c.FirstName;
休息;
案例2:
InitialorderingFunction = c => c.LastName;
休息;
默认值:
InitialorderingFunction = null;
休息;
}

返回InitialorderingFunction;





自定义编辑器Multisort

  public class EditorMultiSort:DataTableMultiSort 
{
保护覆盖表达式< Func< T,字符串>> GetOrderingFunc< T>(int ColumnIndex)
{
Expression< Func< Editor,string>> InitialorderingFunction;
switch(ColumnIndex)
{
case 1:
InitialorderingFunction = c => c.BusinessName;
休息;
案例2:
InitialorderingFunction = c => c.Address;
休息;
默认值:
InitialorderingFunction = null;
休息;
}

返回InitialorderingFunction;
}

}


解决方案

你得到编译器错误的原因是,你已经将通用约束推送到单个方法,而不是将约束放在上。您拥有它的方式无法用返回表达式< Func<用户的方法覆盖内在通用方法 GetOrderingFunc< T> 串GT;>

如果您制作 DataTableMultiSort generic,并且将通用参数从方法中取出, / p>

  public class DataTableMultiSort< T>其中T:IdentityEntity 
{
...
受保护的虚拟表达式< Func< T,字符串>> GetOrderingFunc(int ColumnIndex)
{
return null;
}
}

public class UserMultiSort:DataTableMultiSort< User>
{
保护覆盖表达式< Func<用户,字符串>> GetOrderingFunc(int ColumnIndex)
{
表达式< Func<用户,字符串>> InitialorderingFunction;
...
}
}


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

}

解决方案

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

这篇关于泛型C#依赖于类型的方法组织的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆