使用循环

时间:2017-09-20 20:59:32

标签: c#

在一个文件中有枚举如下:

public enum RelativeFatLevels
{
    Low,
    Medium,
    High
}

在单独的文件中有一个Component类,如下所示:

public class Component
{        
    public Enum RelativeFatLevel;
    public double Fat;
    // other properties here
}

在一个单独的文件中有一个FormulaOutput类,它就像一个容器,用于保存公式的Component对象:

public class FormulaOutput
{
    public Component Low { get; set; }
    public Component Medium { get; set;}
    public Component High { get; set;}
}

在一个单独的单元测试文件中,我正在尝试构建一个通用方法来替换这个逻辑:

private FormulaOutput GetDifferences(FormulaOutput app, FormulaOutput test)
{
    FormulaOutput container = new FormulaOutput();

    Component componentDifferencesLow = new Component();
    componentDifferencesLow.RelativeFatLevel = RelativeFatLevels.Low;

    Component componentDifferencesMedium = new Component();
    componentDifferencesMedium.RelativeFatLevel = RelativeFatLevels.Medium;

    Component componentDifferencesHigh = new Component();
    componentDifferencesHigh.RelativeFatLevel = RelativeFatLevels.High;

    container.Low = componentDifferencesLow;
    container.Medium = componentDifferencesMedium;
    container.High = componentDifferencesHigh;

    return container;
}

这是一个简化版本 - 在Component中有几个属性需要设置,上面没有显示(它们被移除以使代码片段更易读)。

以下是我尝试过的内容。注释掉的行不起作用。我一直坚持如何从这里开始。请注意,该文件包含using System.Reflection;,因此我不认为这是问题所在。此外,将组件添加到容器对象的代码还不存在 - 不知道如何编写它。

private FormulaOutput GetDifferences(FormulaOutput app, FormulaOutput test)
{
    FormulaOutput container = new FormulaOutput();

    foreach (RelativeFatLevels relativeFatLevel in Enum.GetValues(typeof(RelativeFatLevels)))
    {
        Component componentDifferences = new Component();
        //componentDifferences.RelativeFatLevel = test.Low.RelativeFatLevel;
        //componentDifferences.RelativeFatLevel; 
        //PropertyInfo xyz = test.GetType().GetProperty(relativeFatLevel.ToString()).GetValue(test, null);
        //PropertyInfo abc = typeof(FormulaOutput).GetProperty(relativeFatLevel.ToString()).GetValue(test, null);
        //Type type = typeof(FormulaOutput);
        //var aaa = type.GetProperty(relativeFatLevel.ToString()).GetValue(test, null);
        //Component current = typeof(FormulaOutput).GetProperty(relativeFatLevel.ToString()).GetValue(test, null);
    }

    return container;
}

2 个答案:

答案 0 :(得分:1)

这样的东西?

// create container
FormulaOutput container = new FormulaOutput();

foreach (RelativeFatLevels relativeFatLevel in Enum.GetValues(typeof(RelativeFatLevels)))
{
    // create component
    Component component = new Component();
    component.RelativeFatLevel = relativeFatLevel;
    component.Fat = CalculateFatFromRelativeFatLevel(relativeFatLevel);

    // look for property on the container with the same name as the fat level
    var property = typeof(FormulaOutput).GetProperty(relativeFatLevel.ToString());
    if (property != null)
    {
        // assign the component to that property
        property.SetValue(container, component);
    }
}

return container;

注意:您的枚举应该被称为RelativeFatLevel(单数),因为它代表单个值。

答案 1 :(得分:0)

我建议不要试图枚举属性的方法。它实际上会通过向其添加额外的层来使您的代码更难维护。有时通过迭代来分配属性是有意义的,但这不是那个时代之一。

相反,我会创建一个为您生成Component的方法,并且只需调用它3次。

public Component GenerateComponent(RelativeFatLevel level)
{
    var result = new Component();
    result.RelativeFatLevel = level;
    result.Fat = CalculateFat(level);
    //set other properties here.
}

public double CalculateFat(RelativeFatLevel level)
{
    //todo
}

...

container.Low = GenerateComponent(RelativeFatLevel.Low);
container.Medium = GenerateComponent(RelativeFatLevel.Medium);
container.High = GenerateComponent(RelativeFatLevel.High);