简体   繁体   中英

C# Passing Generic Class as a parameter to a custom Method

I'm trying to create a generic function that can take in the following parameters.

  1. Class Type - classType
  2. Page Number - pageNum
  3. Page Size - pageSize
  4. Order by Column - orderByColumn

Below is an example of the function that I'm trying to replicate.

 public List<T> GetDataPerPage<T>(IList<T> classType, int pageNum, int pageSize, string orderByColumn)
       {
        if (pageSize <= 0) pageSize = 10;  // TODO: Default pageSize for the Moment
        if (pageNum <= 0) pageNum = 1;
        int excludedRows = (pageNum - 1) * pageSize;

        return GetRepo<classType>().All(null).AsQueryable().Where(p => p.IsDeleted == false).OrderBy(p => p.orderByColumn).Skip(excludedRows).Take(pageSize).ToList();                                  
    }

I'm uncertain of how it should pass the classType and the orderByColumn to the function.

Any help would be greatly appreciated. Thanks

You do not need to pass the classType parameter at all, because you will declare the type T when you use the GetDataPerPage<T>() method, so change it to this:

public List<T> GetDataPerPage<T>(int pageNum, int pageSize, string orderByColumn)
{
    if (pageSize <= 0) pageSize = 10;  // TODO: Default pageSize for the Moment
    if (pageNum <= 0) pageNum = 1;
    int excludedRows = (pageNum - 1) * pageSize;

    return GetRepo<T>().All(null)
                       .AsQueryable()
                       .Where(p => p.IsDeleted == false)
                       .OrderBy(p => p.orderByColumn)
                       .Skip(excludedRows)
                       .Take(pageSize)
                       .ToList();                                  
}

As for the orderByColumn parameter, if you are going to just do a comparison like your example, then passing in a string is fine, but if you want to control the logic of the comparison to use a string property of type T , then pass a delegate like this:

public List<T> GetDataPerPage<T>(int pageNum, int pageSize, Func<T, string> orderBy)
{
    if (pageSize <= 0) pageSize = 10;  // TODO: Default pageSize for the Moment
    if (pageNum <= 0) pageNum = 1;
    int excludedRows = (pageNum - 1) * pageSize;

    return GetRepo<T>().All(null)
                       .AsQueryable()
                       .Where(p => p.IsDeleted == false)
                       .OrderBy(orderBy)
                       .Skip(excludedRows)
                       .Take(pageSize)
                       .ToList();                                  
}

Usage:

var list = GetDataPerPage<T>(pageNum, pageSize, p => p.Property1);

You already know the "class type".. its T :

GetRepo<T>().All(null)...

For the OrderBy .. pass in a delegate yourself:

public List<T> GetDataPerPage<T>(IList<T> classType, 
                                 int pageNum, 
                                 int pageSize, 
                                 Func<T, object> orderBy)
{
    ...

    return ... ... .OrderBy(orderBy)
}

And pass the delegate in like this:

GetDataPerPage(..., ..., ..., x => x.SomePropertyHere);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM