c#泛型:我可以在这个例子中使用它们吗?
本文关键字:泛型 我可以 | 更新日期: 2023-09-27 18:11:05
如何使用泛型做到这一点?
在我当前的代码中,我为车辆的每个特性编写了如下代码:
foreach (var vaudioSystem in VehicleAudioSystem)
{
var acMl = (VOVehicleFeatureList) FrameworkFactoryApi.CreateVO(typeof (VOVehicleFeatureList));
acMl.IsInitialized = false;
acMl.Initialize(true);
acMl.VehicleFeatureListType = GlobalEnums.VehicleFeatureListType.AudioSystem;
acMl.ValueId = vaudioSystem;
vehicleSpec.VehicleFeatureLists.Add(acMl);
}
foreach(var axle in VehicleAxles)
{
var acMl =
(VOVehicleFeatureList)FrameworkFactoryApi.CreateVO(typeof(VOVehicleFeatureList));
acMl.IsInitialized = false;
acMl.Initialize(true);
acMl.VehicleFeatureListType = GlobalEnums.VehicleFeatureListType.Axles;
acMl.ValueId = axle;
vehicleSpec.VehicleFeatureLists.Add(acMl);
}
foreach(var nav in VehicleNavSystem)
{
var acMl =
(VOVehicleFeatureList)FrameworkFactoryApi.CreateVO(typeof(VOVehicleFeatureList));
acMl.IsInitialized = false;
acMl.Initialize(true);
acMl.VehicleFeatureListType = GlobalEnums.VehicleFeatureListType.NavSystem;
acMl.ValueId = nav;
vehicleSpec.VehicleFeatureLists.Add(acMl);
}
是否有某种方法可以减少使用泛型代码的长度,或者我是在错误的树中吠叫?
你可以简单地编写一个方法,将车辆的特性添加到你的vehiclespec
public void AddVehiculeFeatures(IEnumerable<int> featureList, GlobalEnums.VehicleFeatureListType listType, Vehicle vehicleSpec)
{
foreach(int feature in featureList)
{
var acMl = (VOVehicleFeatureList)FrameworkFactoryApi.CreateVO(typeof(VOVehicleFeatureList));
acMl.IsInitialized = false;
acMl.Initialize(true);
acMl.VehicleFeatureListType = listType;
acMl.ValueId = nav;
vehicleSpec.VehicleFeatureLists.Add(acMl);
}
}
和用法:
AddVehiculeFeatures(axesList, GlobalEnums.VehicleFeatureListType.Axes, vehicleSpec)
AddVehiculeFeatures(otherList, GlobalEnums.VehicleFeatureListType.Other, vehicleSpec)
AddVehiculeFeatures(thirdFeatureList, GlobalEnums.VehicleFeatureListType.third one, vehicleSpec)
您可以添加一个函数,该函数接受列表和枚举值,然后调用它三次。
protected void AddFeaturesFromlist(List<int> featureList, GlobalEnums.VehicleFeatureListType type)
{
foreach(var item in featureList)
{
var acMl =(VOVehicleFeatureList)FrameworkFactoryApi.CreateVO(typeof(VOVehicleFeatureList));
acMl.IsInitialized = false;
acMl.Initialize(true);
acMl.VehicleFeatureListType = type;
acMl.ValueId = item;
vehicleSpec.VehicleFeatureLists.Add(acMl);
}
}
AddFeaturesFromlist(VehicleAudioSystem, GlobalEnums.VehicleFeatureListType.AudioSystem);
...
可能不是泛型,但您肯定可以通过一点方法重构使其更优雅。你重复了很多代码,所以尽量减少。
foreach (var vaudioSystem in VehicleAudioSystem)
AddFeature(vaudioSystem, GlobalEnums.VehicleFeatureListType.AudioSystem);
foreach(var axle in VehicleAxles)
AddFeature(axle, GlobalEnums.VehicleFeatureListType.Axles);
foreach(var nav in VehicleNavSystem)
AddFeature(nav, GlobalEnums.VehicleFeatureListType.NavSystem);
[...]
public static void AddFeature(int id, VehicleFeatureListType type)
{
var acMl = (VOVehicleFeatureList)FrameworkFactoryApi.CreateVO(
typeof(VOVehicleFeatureList));
acMl.IsInitialized = false;
acMl.Initialize(true);
acMl.VehicleFeatureListType = type;
acMl.ValueId = id;
vehicleSpec.VehicleFeatureLists.Add(acMl);
}
如果你真的想使用泛型,这里有一个使用LINQ的版本:)
foreach (var item in
VehicleAudioSystem.Select(id => new { id, type = GlobalEnums.VehicleFeatureListType.AudioSystem }).Concat(
VehicleAxles.Select(id => new { id, type = GlobalEnums.VehicleFeatureListType.Axles }).Concat(
VehicleNavSystem.Select(id => new { id, type = GlobalEnums.VehicleFeatureListType.NavSystem }))))
{
var acMl = (VOVehicleFeatureList)FrameworkFactoryApi.CreateVO(typeof(VOVehicleFeatureList));
acMl.IsInitialized = false;
acMl.Initialize(true);
acMl.VehicleFeatureListType = item.type;
acMl.ValueId = item.v;
vehicleSpec.VehicleFeatureLists.Add(acMl);
}
通用解决方案:
首先,创建一个名为VehicleFeature的类,如下所示:
public class VehicleFeature
{
public int ValueId;
public GlobalEnums.VehicleFeatureListType VehicleFeatureListType;
public VehicleFeature(int valueId, GlobalEnums.VehicleFeatureListType vehicleFeatureListType)
{
this.ValueId = valueId;
this.VehicleFeatureListType = vehicleFeatureListType;
}
}
然后,创建一个名为VehicleFeatureList的类,继承List:
public class VehicleFeatureList : List<VehicleFeature>
{
public GlobalEnums.VehicleFeatureListType VehicleFeatureListType { get; set; }
private List<VehicleFeature> values;
public List<VehicleFeature> Values
{
get { return values; }
set { values = value; }
}
public void Add(int ValueId)
{
this.values.Add(new VehicleFeature(ValueId, this.VehicleFeatureListType));
}
public void AddRange(int[] values)
{
foreach (int item in values)
{
this.Add(item);
}
}
public VehicleFeatureList(GlobalEnums.VehicleFeatureListType vehicleFeatureListType)
{
this.VehicleFeatureListType = vehicleFeatureListType;
}
}
接下来,你的变量"VehicleAudioSystem", "VehicleAxles"answers"VehicleNavSystem"不会是List,而只是类型为VehicleFeatureList。别忘了每次都输入正确的特性类型:
VehicleFeatureList VehicleAudioSystem = new VehicleFeatureList(GlobalEnums.VehicleFeatureListType.AudioSystem);
VehicleFeatureList VehicleAxles = new VehicleFeatureList(GlobalEnums.VehicleFeatureListType.Axles);
VehicleFeatureList VehicleNavSystem = new VehicleFeatureList(GlobalEnums.VehicleFeatureListType.NavSystem);
你可以像这样给变量添加一个值:
VehicleAxles.Add(5);
如果你想在同一类型中添加不同的值,你可以这样做:
VehicleAudioSystem.AddRange(new int[] {1,5,6,9});
然后,你有这个独特的函数可以用于任何你想要的功能:
private void AddToVehicleFeatureList<T>(T VehicleParameterType)
where T : VehicleFeatureList
{
foreach (var item in VehicleParameterType)
{
var acMl =
(VOVehicleFeatureList)FrameworkFactoryApi.CreateVO(typeof(VOVehicleFeatureList));
acMl.IsInitialized = false;
acMl.Initialize(true);
acMl.VehicleFeatureListType = item.VehicleFeatureListType;
acMl.ValueId = item.ValueId;
vehicleSpec.VehicleFeatureLists.Add(acMl);
}
}
你像这样调用方法:
AddToVehicleFeatureList(VehicleAudioSystem);
您可以在您的GlobalEnums中添加任何您想要的特性类型。VehicleFeatureListType,代码仍然可以工作。问候