我有两个派生自同一基类的类
public class AugInfo
{
public string UserId { get; set; }
public string Email { get; set; }
public bool EmailConfirmed { get; set; }
public string PhoneNumber { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public string Name { get { return FirstName + " " + LastName; } }
public string Company { get; set; }
public string City { get; set; }
public string State { get; set; }
public Boolean LockedOut { get; set; }
public string Country { get; set; }
public int ModelsCount { get; set; }
public DateTime CreateDate { get; set; }
public string LoginProvider { get; set; }
public AugInfo() { }
}
public class AdminUserInfo : AugInfo
public class GroupUserInfo : AugInfo
下面描述了两个函数
protected List<AdminUserInfo> FilterUsers(List<AdminUserInfo> users,SearchViewModel<AdminUserInfo, EnumUserSort> search)
{
if (!string.IsNullOrEmpty(search.SearchString))
{
users = users.Where(d =>
d.Email.ToLower().Contains(search.SearchString.ToLower())
|| d.Name.ToString().Contains(search.SearchString) ||
d.Company.ToString().ToLower().Contains(search.SearchString.ToLower())).ToList();
}
return users;
}
protected List<GroupUserInfo> FilterUsers(List<GroupUserInfo> users,SearchViewModel<GroupUserInfo, EnumUserSort> search)
{
if (!string.IsNullOrEmpty(search.SearchString))
{
users = users.Where(d => d.Email.ToLower().Contains(search.SearchString.ToLower())
|| d.Name.ToString().Contains(search.SearchString) ||
d.Company.ToString().ToLower().Contains(search.SearchString.ToLower())).ToList();
}
return users;
}
在这个函数中,我只使用那些属于AugInfo
的属性。
我有一种方法可以使用Polymorphism
或Generic types
将它们组合成一个函数。我无法做到这一点。
我试图声明一个像这样的函数
protected List<AugInfo> FilterUsers(List<AugInfo> users,SearchViewModel<AugInfo, EnumUserSort> search)
{
if (!string.IsNullOrEmpty(search.SearchString))
{
users = users.Where(d => d.Email.ToLower().Contains(search.SearchString.ToLower())
|| d.Name.ToString().Contains(search.SearchString) ||
d.Company.ToString().ToLower().Contains(search.SearchString.ToLower())).ToList();
}
return users;
}
但不幸的是我无法调用该功能
List<AdminUserInfo> users= FilterUsers(users, search);
我收到了会员错误
答案 0 :(得分:2)
您需要T
上的egeneric约束,以便只有从AugInfo
派生的类型才能用作通用参数:
protected List<T> FilterUsers<T>(List<T> users, SearchViewModel<T, EnumUserSort> search) where T: AugInfo
{
if (!string.IsNullOrEmpty(search.SearchString))
{
users = users.Where(d =>
d.Email.ToLower().Contains(search.SearchString.ToLower())
|| d.Name.ToString().Contains(search.SearchString)
|| d.Company.ToString().ToLower().Contains(search.SearchString.ToLower())).ToList();
}
return users;
}
答案 1 :(得分:2)
应该在这里使用泛型:
protected List<T> FilterUsers<T>(List<T> users, SearchViewModel<T, EnumUserSort> search) where T : AugInfo {
if (!string.IsNullOrEmpty(search.SearchString)) {
users = users.Where(d =>
d.Email.ToLower().Contains(search.SearchString.ToLower())
|| d.Name.ToString().Contains(search.SearchString) ||
d.Company.ToString().ToLower().Contains(search.SearchString.ToLower())).ToList();
}
return users;
}
仅仅写List<AugInfo>
是不够的,因为List<T>
不是逆变和协变的同时(C#中不存在这种情况)。您无法将List<BaseType>
转换为List<DerivedType>
。