C#泛型:我可以在这个例子中使用它们吗?

时间:2011-10-13 13:39:17

标签: c# .net generics .net-4.0

如何使用泛型?

在我目前的代码中,我正在为车辆上的每个功能编写类似的内容。

        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);
            }

有没有我可以使用Generics减少这段代码的长度,还是我咆哮错误的树?

5 个答案:

答案 0 :(得分:4)

您可以简单地编写一种方法,将车辆功能添加到您的vehiclepec

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)

答案 1 :(得分:1)

您可以添加一个功能,它会获取您的列表和枚举值,然后调用它三次。

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);
...

答案 2 :(得分:0)

也许不是泛型,但你可以通过一些方法重构使它更优雅。你正在重复很多代码,所以尽量减少它。

    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);
}

答案 3 :(得分:0)

如果你真的想使用泛型,那么这是一个使用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);
}

答案 4 :(得分:0)

通用解决方案:

首先,按如下方式创建一个名为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”和“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中添加所需的任何要素类型,代码仍然有效。 问候