使用父项的一个属性获取特定类的所有属性

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用父项的一个属性获取特定类的所有属性相关的知识,希望对你有一定的参考价值。

请考虑以下代码:

class ClassA
{
    public int ID { get; set; }
    public string Name { get; set; }
    public ClassA PropA { get; set; }
}

class ClassC
{
    public ClassC()
    {
        var MyVar = new List<ClassA>
        {
            new ClassA()
            {
                ID = 1,
                Name = "Name1",
                PropA = new ClassA()
                {
                    ID = 2,
                    Name = "Name2",
                    PropA = new ClassA()
                    {
                        ID = 3,
                        Name = "Name3",
                        PropA = new ClassA()
                        {
                            ID =4,
                            Name = "Name4",
                            PropA = null,
                        }
                    }

                }
            },
            new ClassA()
            {
                ID = 5,
                Name = "Name5",
                PropA = new ClassA()
                {
                    ID = 6,
                    Name = "Name6",
                    PropA = null,
                }
            },
            new ClassA()
            {
                ID = 7,
                Name = "Name7",
                PropA = null,
            },
            new ClassA()
            {
                ID = 8,
                Name = "Name8",
                PropA = new ClassA()
                {
                    ID = 9,
                    Name = "Name9",
                    PropA = new ClassA()
                    {
                        ID = 10,
                        Name = "Name10",
                        PropA = new ClassA()
                        {
                            ID = 11,
                            Name = "Name11",
                            PropA = null,
                        }
                    }
                }
            },
        };
    }
}

我想获得非空的所有ClassA属性以及其父级的name属性。例如:

   Parent        Class A
--------------------------------
1)"Name1"     Class A with ID = 2
2)"Name2"     Class A with ID = 3
3)"Name3"     Class A with ID = 4
4)"Name5"     Class A with ID = 6
and...

谢谢

答案

你的意思是这样的(C#7):

private static IEnumerable<string> Describe(ClassA a)
{
    var (parent, current) = (a, a.PropA);

    while (!(current is null))
    {
        yield return $"{parent.Name}     Class A with ID = {current.ID}";
        (parent, current) = (current, current.PropA);
    }
}
...
var results = MyVar.SelectMany(Describe);

根据评论中的要求,C#4版本:

private static IEnumerable<string> DescribeCSharp4(ClassA a)
{
    var parent = a;
    var current = a.PropA;

    while (current != null)
    {
        yield return parent.Name + "     Class A with ID = " + current.ID;

        parent = current;
        current = current.PropA;
    }
}
...
var results = MyVar.SelectMany(DescribeCSharp4);

以上是关于使用父项的一个属性获取特定类的所有属性的主要内容,如果未能解决你的问题,请参考以下文章

访问父项的属性不起作用(QML)

ToolStripMenuItem 相对于父项的位置

EF 获取类的属性并排除特定属性(getType().GetProperties())

使用特定对象而不是对象属性获取核心数据

Core Data 中父实体的计算属性

qml中子属性的绑定属性