我有以下通用类:
public class SearchModel<T>
{
public string Name { get; set; }
public int? UserId { get; set; }
public List<T> result { get; set; }
}
public class A{
..
..
}
public class B{
..
..
}
SearchModel 类中的 List 可以是 A/B 类型。现在我有这两个函数调用,它们给了我适当的结果。
public List<A> SearchApplicationsForA(SearchModel<A> model){
}
public List<B> SearchApplicationsForB(SearchModel<B> model){
}
我想知道我是否可以编写一个通用函数来识别 T 的类型并调用相应的函数。例如。
public List<T> SearchApplications<T>(SearchModel<T> model)
{
if (typeof(T) == typeof(A))
{
return SearchVerificationsForA(model);
}
else if (typeof(T) == typeof(B))
{
return SearchApplicationsForB(model);
}
}
是否可以编写这样的函数?