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

C# 从C语言中的列表访问对象参数,c#,grasshopper,C#,Grasshopper,我正在创建一个代理对象列表,其中包含许多不同的参数,但我不确定如何使用循环访问所有对象的特定参数…我希望做的是从所有代理获取所有Point3d位置。我该怎么做 // Define Agent class class Agent { Point3d Pos = new Point3d(); Vector3d Vec = new Vector3d(); int Alignment; double Separation; double Cohesion;

我正在创建一个代理对象列表,其中包含许多不同的参数,但我不确定如何使用循环访问所有对象的特定参数…我希望做的是从所有代理获取所有Point3d位置。我该怎么做

// Define Agent class
class Agent
{
    Point3d Pos = new Point3d();
    Vector3d Vec = new Vector3d();
    int Alignment;
    double Separation;
    double Cohesion;
    double NeighborRadius;

    public Agent(Point3d pos, Vector3d vec, int alignment, double separation, double cohesion, double neighborRadius)
    {
        Pos = pos;
        Vec = vec;
        Alignment = alignment;
        Separation = separation;
        Cohesion = cohesion;
        NeighborRadius = neighborRadius;
    }
}

protected override void SolveInstance(IGH_DataAccess DA)
{
    // Initialize Agents
    for (int i = 0; i < agents; i++)
    {
        double xPos = RandomfromDouble(0.0, boundx);
        double yPos = RandomfromDouble(0.0, boundy);
        double zPos = RandomfromDouble(0.0, boundz);

        Point3d pos = new Point3d(xPos, yPos, zPos);        // Create Agent Start Position
        Vector3d vec = new Vector3d(xPos + 1, yPos, zPos);  // Create Agent Start Vector

        Agent agent = new Agent(pos, vec, alignment, separation, cohesion, neighborRadius);
        allAgents.Add(agent);
        agentPositions.Add(pos);
    }
}

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

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


注意:如果您可以更改Pos的访问修饰符,Linq可从.Net Framework 3.5获得:

class Agent
{
    public Point3d Pos = new Point3d();
    //.
    //.
    //.
}
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
}


注意:Linq可从.Net Framework 3.5获得,您无法访问Point3d Pos,因为默认情况下它是私有的。因此,请使用如下所示的公共访问修饰符,并希望它能解决此问题:

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
}
public Point3d Pos = new Point3d();

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

public Point3d Pos = new Point3d();
allAgents.Selectz=>z.PosallAgents.Selectz=>z.Pos