I have the following Point and Class2 classes. My goal is to restore all points in Class2 instances in order to keep them in the list.
public class Point { int x; int y; string col; public Point(int abs, int ord, string clr) { this.x = abs; this.y = ord; this.col = clr; } public string toColor() { return this.col; } public int Addition() { return (this.x + this.y); } } class Class2 { int test; Point pt1; Point pt2; Point pt3; List<Point> listPt = new List<Point>() { }; public Class2() { test = 100; this.pt1 = new Point(2, 3, "red"); this.pt2 = new Point(1, 30, "blue"); this.pt3 = new Point(5, 10, "black"); } public List<Point> getAllPoint() { foreach (var field in this.GetType().GetFields()) {
But this does not work, because the field is of type "System.Reflection.FieldInfo", how can I do this? I read a lot of articles, but I did not find a solution:
http://msdn.microsoft.com/en-us/library/ms173105.aspx
Type conversion error when setting property through reflection
http://technico.qnownow.com/how-to-set-property-value-using-reflection-in-c/
Convert a variable to a type only known at runtime?
...
(I want to do this: at the end, the class will have Point instances depending on db, so I cannot know how many points I will have, and I will need to run a member function, for example, Addition.)
Thanks for all the ideas!
source share