从C#中的列表访问对象参数

时间:2018-10-29 05:38:59

标签: c# grasshopper

我正在创建一个包含许多不同参数的代理对象列表,但是我不确定如何使用循环访问我所有对象的特定参数...我要寻找的是要做的就是从我所有的代理商那里获得所有Point3d职位。我该怎么办?

dt$priority <- with(dt, ave(lob, id, month, FUN = function(x) match(x, unique(x))))

3 个答案:

答案 0 :(得分:2)

如果您可以更改Pos的访问修饰符:

class Agent
{
    public Point3d Pos = new Point3d();
    //.
    //.
    //.
}

class Agent
{
    public Agent()
    {
       Pos = new Point3d();
    }
    public Point3d Pos { get;private set; }
    //.
    //.
    //.
}

List<Agent> allAgents = new List<Agent>();
List<Point3d> agentPositions = new List<Point3d>();

// Initialize Agents
//.
//.
//.


agentPositions = allAgents
            .Select(agent => agent.Pos)
            .ToList();

注意:Linq可从.Net Framework 3.5获得

答案 1 :(得分:2)

class Agent
{
    public Point3d Pos {get; private set;}
    public Agent() 
    {
        Pos = new Point3d();
    }
    ....
}
foreach (Agent ag in allAgents)
{
    Console.WriteLine(ag.Pos); //might need to dereference a specific member like x,y, or z
}

答案 2 :(得分:0)

您无法访问Point3d Pos,因为默认情况下它是私有的。因此,请使用如下所示的公共访问修饰符,希望它可以解决此问题:

public Point3d Pos = new Point3d();