我似乎对对象的引用有一个意想不到的问题。我的代码有一个草稿:
// file 1
List<A> list1;
List<B> list2 = function(list1);
//file 2
public static List<B> function(List<A> input)
{
list<B> output = new List<B>();
for (int i ...)
{
output.Add(new B(input[i]));
{
return output;
}
// file 1
list1[1].FieldOfAObject = 10; // I change an object from list1
WriteLine(list2[1].objectA.FieldOfAObject); // it is not changed
在我看来,由于list1是一个类而A是一个类,并且它们是通过引用传递的,因此在function
中创建并分配给list2
的类型B的对象应该保留关于list1
的对象的参考。
修改 真实代码:
public void PrintCylinder(Camera camera, Cylinder cylinder, Bitmap Bitmap, double bitmapWidth, double bitmapHeight, PrintArguments pa)
{
...
List<AffineCoordinates> verticesToPritOnScreen = new List<AffineCoordinates>(); // out list1
foreach (var vertex in cylinder.Mesh.MeshVertices)
{
AffineCoordinates newVertex = PVM * vertex;
verticesToPritOnScreen.Add(newVertex); // list1 is being filled here
}
List<Face> FaceList = MeshGridGenerator.GenerateCylinderGrid(verticesToPritOnScreen, cylinder.D); // our list2
// CHANGES BENEATH ARE NOT VISIBLE IN FaceList objects
for (int i = 0; i < verticesToPritOnScreen.Count; ++i)
{
// project to NDC
verticesToPritOnScreen[i] = verticesToPritOnScreen[i] / verticesToPritOnScreen[i].W;
// project to screen space
verticesToPritOnScreen[i].X = (verticesToPritOnScreen[i].X + 1) * bitmapWidth / 2;
verticesToPritOnScreen[i].Y = (verticesToPritOnScreen[i].Y + 1) * bitmapHeight / 2;
verticesToPritOnScreen[i].Z = (verticesToPritOnScreen[i].Z + 1) / 2;
verticesToPritOnScreen[i].W = 1;
}
...
}
// our function (from MeshGridGenerator class)
public static List<Face> GenerateCylinderGrid(List<AffineCoordinates> vertices, int D)
{
List<Face> FaceList = new List<Face>();
for (int i = 1; i <= D; ++i)
{
// upper base
FaceList.Add(new Face(vertices[0], vertices[i], vertices[i % D + 1]));
// bottom base
FaceList.Add(new Face(vertices[vertices.Count - 1], vertices[i % D + 1 + D], vertices[i + D]));
// side wall's upper triangle
FaceList.Add(new Face(vertices[i], vertices[i + D], vertices[i % D + 1]));
// side wall's bottom triangle
FaceList.Add(new Face(vertices[i % D + 1], vertices[i + D], vertices[i % D + 1 + D]));
}
return FaceList;
}
// our B class
public class Face
{
public AffineCoordinates[] vertices;
public bool AreVerticesClockwise
{
get
{
double sum = 0.0;
for (int i = 0; i < 3; ++i)
{
sum += (vertices[(i + 1) % 3].X - vertices[i].X) * (vertices[(i + 1) % 3].Y + vertices[i].Y);
}
return sum < 0;
}
}
public Face(params AffineCoordinates[] ver)
{
vertices = new AffineCoordinates[3];
vertices[0] = ver[0];
vertices[1] = ver[1];
vertices[2] = ver[2];
}
}
我知道这是很多颂歌,这就是为什么我不想发布它。
答案 0 :(得分:1)
这段代码打印'20',这是我认为你想要的?
public class A
{
public int Value;
}
public class B
{
public A objectA;
public B(A obj)
{
objectA = obj;
}
}
class Program
{
public static List<B> function(List<A> input)
{
List<B> output = new List<B>();
foreach (var i in input)
{
output.Add(new B(i));
}
return output;
}
static void Main(string[] args)
{
List<A> listA = new List<A>();
listA.Add(new A { Value = 10 });
List<B> listB = function(listA);
listA[0].Value = 20;
Console.WriteLine(listB[0].objectA.Value);
}
}