是否可以在不传递和存储引用的情况下从成员对象访问对象?在下面的示例中,给定的椅子对象是否可以访问房屋对象,而房屋不必将其引用向下传递给成员层次结构?
public class Chair {
public string Material { get; set; }
public Chair() {
Material = "Wood";
}
public bool IsInMiami() {
// Get instance of House where chair is found
House house = ... // Reflection?
return house.City.Equals("Miami");
}
}
public class Room {
private List<Chair> _chairs;
public Room() {
_chairs = new List<Chair>();
_chairs.Add(new Chair());
}
}
public class House {
private List<Room> _rooms;
public string City { get; set; }
public House() {
_rooms = new List<Room>();
_rooms.Add(new Room());
City = "Orlando";
}
}
答案可能是通过反思,但我不知道如何做到这一点,或者是否有另一种方法来实现同样的目标。
提前致谢