对象类...
class Room { public string Value1 { get; set; } public string Value2 { get; set; } public string Value3 { get; set; } public Room() { this.Value1 = "one"; this.Value2 = "two"; this.Value3 = "three"; } } class Building { public Room Room-Bob { get; set; } public Room Room-Steve{ get; set; } public Building() { this.Room-Bob = new Room(); this.Room-Steve = new Room(); } } class Street { public Building Building-Black{ get; set; } public Building Building-Blue { get; set; } public Building Building-Yellow { get; set; } public Building Building-White { get; set; } public Street () { this.Building-Black = new Building(); this.Building-Blue = new Building(); this.Building-Yellow = new Building(); this.Building-White = new Building(); } }我目前使用什么来获取值...
class go { public void go() { string SelectedValue = ""; Street s = new Street(); string PathToProperty = "s.Building1.Room1.value1"; if(PathToProperty == "s.Building1.Room1.value1") { SelectedValue = s.Building1.Room1.Value1; } if (PathToProperty == "s.Building1.Room1.value2") { SelectedValue = s.Building1.Room1.Value2; } } }我如何获得价值......或类似的东西
string PathToProperty = "s.Building1.Room1.value1"; SelectedValue = PathToProperty;我也想设置这样的属性...
字符串PathToProperty =“s.Building1.Room1.value1”; SelectedValue = PathToProperty;
原因是我通过将一堆组合框中的文本串在一起来制作PathToProperty。 最终,我想避免随着组合框内的选项的增加,不得不添加到我的IF语句列表中。
我一直在讨论反射,但想避免这种情况,我读了一些地方可以用接口来做到这一点(使用它们来公开属性),但我不知道如何。
如果Reflection是这个最好的选择,有人可以告诉我两种获得财产的方法,另一种方法来设置它?
The Object Classes...
class Room { public string Value1 { get; set; } public string Value2 { get; set; } public string Value3 { get; set; } public Room() { this.Value1 = "one"; this.Value2 = "two"; this.Value3 = "three"; } } class Building { public Room Room-Bob { get; set; } public Room Room-Steve{ get; set; } public Building() { this.Room-Bob = new Room(); this.Room-Steve = new Room(); } } class Street { public Building Building-Black{ get; set; } public Building Building-Blue { get; set; } public Building Building-Yellow { get; set; } public Building Building-White { get; set; } public Street () { this.Building-Black = new Building(); this.Building-Blue = new Building(); this.Building-Yellow = new Building(); this.Building-White = new Building(); } }What I am currently using to get the values...
class go { public void go() { string SelectedValue = ""; Street s = new Street(); string PathToProperty = "s.Building1.Room1.value1"; if(PathToProperty == "s.Building1.Room1.value1") { SelectedValue = s.Building1.Room1.Value1; } if (PathToProperty == "s.Building1.Room1.value2") { SelectedValue = s.Building1.Room1.Value2; } } }How I would like to get the values... or something similar
string PathToProperty = "s.Building1.Room1.value1"; SelectedValue = PathToProperty;I would also like to set the property like this...
string PathToProperty = "s.Building1.Room1.value1"; SelectedValue = PathToProperty;
The reason being I am making the the PathToProperty by stringing together the text from a bunch of combo boxes. Ultimately I want to avoid having to add to my list of IF statements as the options inside the combo boxes increase.
I have being messing around with reflection but would like to avoid this , I read somewhere you can do this with interfaces (using them to expose properties) but I do not know how.
If Reflection is the best choice for this can someone show me 2 methods of to get the property and another to set it?
最满意答案
我建议你采取不同的方法。 IMO反思不是这种情况的方法。
从这开始,然后从中构建 - 重新设计/重构其他部分,当然:
class Room { // same as yours } class Building { public List<Room> Rooms { get; set; } public Building() { Rooms = new List<Room>(); Rooms.Add(new Room()); Rooms.Add(new Room()); // get "room #x" -> var room = objBuilding.Rooms[x]; // get "room #x in building #i" -> var room = objStreet.Buildings[i].Rooms[x]; } } class Street { public List<Building> Buildings { get; set; } public Street () { Buildings = new List<Building>(); Buildings.Add(new Building()); Buildings.Add(new Building()); Buildings.Add(new Building()); Buildings.Add(new Building()); // get "building #i" -> var building = objStreet.Buildings[i]; } }I'd suggest you take a different approach. IMO reflection is not the way to go for these kinds of situations.
Start with this, then build from it - redesigning/refactoring other parts as well, of course:
class Room { // same as yours } class Building { public List<Room> Rooms { get; set; } public Building() { Rooms = new List<Room>(); Rooms.Add(new Room()); Rooms.Add(new Room()); // get "room #x" -> var room = objBuilding.Rooms[x]; // get "room #x in building #i" -> var room = objStreet.Buildings[i].Rooms[x]; } } class Street { public List<Building> Buildings { get; set; } public Street () { Buildings = new List<Building>(); Buildings.Add(new Building()); Buildings.Add(new Building()); Buildings.Add(new Building()); Buildings.Add(new Building()); // get "building #i" -> var building = objStreet.Buildings[i]; } }更多推荐
发布评论