C# 使用循环中的枚举值从对象获取属性

C# 使用循环中的枚举值从对象获取属性,c#,C#,在一个文件中有如下枚举: public enum RelativeFatLevels { Low, Medium, High } 在单独的文件中有一个组件类,如下所示: public class Component { public Enum RelativeFatLevel; public double Fat; // other properties here } 在单独的文件中有一个FormulaOutput类,其作用类似

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

public enum RelativeFatLevels
{
    Low,
    Medium,
    High
}
在单独的文件中有一个组件类,如下所示:

public class Component
{        
    public Enum RelativeFatLevel;
    public double Fat;
    // other properties here
}
在单独的文件中有一个FormulaOutput类,其作用类似于容器,用于保存公式的组件对象:

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;
}
这是一个精简版本-组件中有几个属性需要设置,但上面没有显示(它们被删除以使代码段更具可读性)

下面是我试过的。注释掉的行不工作。我被困在如何从这里开始。请注意,该文件确实包含使用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;
}
像这样的

// 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;

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

我建议不要尝试枚举属性。通过添加额外的层,它实际上会使代码更难维护。有时,通过对属性进行迭代来分配属性是有意义的,但这不是其中之一

相反,我会创建一个方法,为您生成一个
组件
,然后只调用它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
}


当你说不工作时,到底发生了什么?你到底想做什么?什么是nt工作?
公共枚举相对级别
应该是
公共相对级别相对级别
您希望通过此系统实现什么?我不太明白这个想法。尝试用伪代码或文字详细说明您的意图,作为对显示代码的补充。我想写一行通用代码来替换
componentDifferencesLow.relativefattlevel=relativefattlevels.Low;'和'components differencesMedium.RelativeFatLevel=RelativeFatLevels.Medium'和'componentDifferencesHigh.RelativeFattlevel=RelativeFattlevels.High;`。在您最后的评论中,框架设计指南第59页第3.5.3节说:“除非枚举的值是位字段,否则枚举必须使用单数类型名称。”@poke Brilliant!
container.Low = GenerateComponent(RelativeFatLevel.Low);
container.Medium = GenerateComponent(RelativeFatLevel.Medium);
container.High = GenerateComponent(RelativeFatLevel.High);