我有三个类:BaseInfo
、CarInfo
和 PetInfo
。 BaseInfo
类是基类。 CarInfo
继承自 BaseInfo
,并具有与汽车相关的属性。最后,PetInfo
也继承自 BaseInfo
,并具有与宠物相关的属性:
public class BaseInfo
{
[JsonIgnore]
public int UserId { get; set; }
[JsonIgnore]
public string UserName { get; set; }
[JsonIgnore]
public DateTime RegistrationDate { get; set; }
[JsonIgnore]
public DateTime LastUpdated { get; set; }
}
public class CarInfo : BaseInfo
{
[JsonPropertyName("car_make")]
public string Make { get; set; }
[JsonPropertyName("car_model")]
public string Model { get; set; }
…
}
public class PetInfo : BaseInfo
{
[JsonPropertyName("pet_breed")]
public string Breed { get; set; }
[JsonPropertyName("pet_weight")]
public long Weight { get; set; }
…
}
然后我有一个服务,其目的是检索数据,并且在所讨论的方法的情况下,为特定用户创建汽车或宠物的列表:
public class SomeService : ISomeService
{
public List<ANY_OF_THE_DERIVED_CLASSES> GetInfoList(string type, int userId)
{
List<ANY_OF_THE_DERIVED_CLASSES> infoList = new List<ANY_OF_THE_DERIVED_CLASSES>;
switch (type)
{
case "car":
infoList = GetCarList(userId);
break;
case "pet":
infoList = GetPetList(userId);
break;
default:
break;
}
return infoList;
}
private List<CarInfo> GetCarList(int userId)
{
var carList = new List<CarInfo>();
//Logic that receives manipulates and serializes data against CarInfo and adds to list
return carList;
}
private List<PetInfo> GetPetList(int userId)
{
var petList = new List<PetInfo>();
//Logic that receives manipulates and serializes data against PetInfo and adds to list
return petList;
}
}
我还为 SomeService
创建了一个接口,其定义为:
public interface ISomeService
{
List<ANY_OF_THE_DERIVED_CLASSES> GetInfoList(string type, int userId)
}
是否可以让 GetInfoList
方法能够返回 List<CarInfo>
或 List<PetInfo>
?
答案 0 :(得分:2)
@Camilo-Terevinto 在评论中的指导完全正确,但如果您不熟悉泛型,可能会给您留下一些额外的问题。为了提供更全面的示例,您的代码(如您所提议的那样)应该如下所示:
ISomeService
public interface ISomeService
{
List<T> GetInfoList<T>(int userId) where T : BaseInfo;
}
SomeService
public class SomeService: ISomeService
{
public List<T> GetInfoList<T>(int userId) where T : BaseInfo
{
List<T> infoList;
switch (nameof(T))
{
case nameof(CarInfo):
infoList = GetCarList(userId) as List<T>;
break;
case nameof(PetInfo):
infoList = GetPetList(userId) as List<T>;
break;
default:
infoList = new List<T>();
break;
}
return infoList;
}
…
}
您可以使用例如验证这是否有效以下测试类:
[TestClass]
public class SomeServiceTests
{
[TestMethod]
public void GetInfoList_ShouldReturn_ListCarInfo()
{
var service = new SomeService();
var list = service.GetInfoList<CarInfo>(1);
list.Add(new CarInfo());
Assert.IsNotNull(list);
Assert.AreEqual<int>(1, list.Count);
}
List<CarInfo>
时 List<T>
到 infoList
。type
作为参数传入,因为您可以根据 T
类型参数(例如通过 nameof(T)
)来确定。infoList
,因为在大多数情况下,它会立即被替换为例如的结果GetCarList()
。Collection<T>
而不是 List<T>
(source)。这与您最初的问题无关,但使用 C# 8.0 的模式匹配,您可以稍微清理一下 switch 语句:
public List<T> GetInfoList<T>(int userId) where T : BaseInfo =>
nameof(T) switch
{
nameof(CarInfo) => GetCarList(userId) as List<T>,
nameof(PetInfo) => GetPetList(userId) as List<T>,
_ => new List<T>()
};